Skip to content

Commit

Permalink
Rollup merge of #100921 - ChayimFriedman2:and-eager-eval, r=JohnTitor
Browse files Browse the repository at this point in the history
Add a warning about `Option/Result::and()` being eagerly evaluated

Copied from `or()`.

Inspired by [this StackOverflow question](https://stackoverflow.com/questions/73461846/why-is-in-rust-the-expression-in-option-and-evaluated-if-option-is-none).

[The PR for `or()`](#46548) mentions the Clippy lint `or_fun_call` which doesn't exist for `and()` (although there is `unnecessary_lazy_evaluations`). I still think this warning is also good for `and()`. Feel free to close if you disagree.
  • Loading branch information
JohnTitor authored Aug 24, 2022
2 parents 0fd4a74 + eb2fdd9 commit df354f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,12 @@ impl<T> Option<T> {

/// Returns [`None`] if the option is [`None`], otherwise returns `optb`.
///
/// Arguments passed to `and` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use [`and_then`], which is
/// lazily evaluated.
///
/// [`and_then`]: Option::and_then
///
/// # Examples
///
/// ```
Expand Down
5 changes: 5 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,11 @@ impl<T, E> Result<T, E> {

/// Returns `res` if the result is [`Ok`], otherwise returns the [`Err`] value of `self`.
///
/// Arguments passed to `and` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use [`and_then`], which is
/// lazily evaluated.
///
/// [`and_then`]: Result::and_then
///
/// # Examples
///
Expand Down

0 comments on commit df354f5

Please sign in to comment.