Skip to content

Commit 8612db8

Browse files
committed
Add #[must_use] to Option and Result overlap methods
This helps users to avoid dropping errors/`None` without checking, for example Miserlou/Loop/pull/66.
1 parent 9722952 commit 8612db8

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

library/core/src/option.rs

+3
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ impl<T> Option<T> {
527527
/// let x: Option<&str> = None;
528528
/// assert_eq!(x.ok_or(0), Err(0));
529529
/// ```
530+
#[must_use]
530531
#[inline]
531532
#[stable(feature = "rust1", since = "1.0.0")]
532533
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
@@ -553,6 +554,7 @@ impl<T> Option<T> {
553554
/// let x: Option<&str> = None;
554555
/// assert_eq!(x.ok_or_else(|| 0), Err(0));
555556
/// ```
557+
#[must_use]
556558
#[inline]
557559
#[stable(feature = "rust1", since = "1.0.0")]
558560
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E> {
@@ -1241,6 +1243,7 @@ impl<T, E> Option<Result<T, E>> {
12411243
/// let y: Option<Result<i32, SomeErr>> = Some(Ok(5));
12421244
/// assert_eq!(x, y.transpose());
12431245
/// ```
1246+
#[must_use]
12441247
#[inline]
12451248
#[stable(feature = "transpose_result", since = "1.33.0")]
12461249
pub fn transpose(self) -> Result<Option<T>, E> {

library/core/src/result.rs

+2
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ impl<T, E> Result<T, E> {
381381
/// let x: Result<u32, &str> = Err("Nothing here");
382382
/// assert_eq!(x.ok(), None);
383383
/// ```
384+
#[must_use]
384385
#[inline]
385386
#[stable(feature = "rust1", since = "1.0.0")]
386387
pub fn ok(self) -> Option<T> {
@@ -408,6 +409,7 @@ impl<T, E> Result<T, E> {
408409
/// let x: Result<u32, &str> = Err("Nothing here");
409410
/// assert_eq!(x.err(), Some("Nothing here"));
410411
/// ```
412+
#[must_use]
411413
#[inline]
412414
#[stable(feature = "rust1", since = "1.0.0")]
413415
pub fn err(self) -> Option<E> {

0 commit comments

Comments
 (0)