Skip to content

Commit df354f5

Browse files
authored
Rollup merge of #100921 - ChayimFriedman2:and-eager-eval, r=JohnTitor
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.
2 parents 0fd4a74 + eb2fdd9 commit df354f5

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

library/core/src/option.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,12 @@ impl<T> Option<T> {
11891189

11901190
/// Returns [`None`] if the option is [`None`], otherwise returns `optb`.
11911191
///
1192+
/// Arguments passed to `and` are eagerly evaluated; if you are passing the
1193+
/// result of a function call, it is recommended to use [`and_then`], which is
1194+
/// lazily evaluated.
1195+
///
1196+
/// [`and_then`]: Option::and_then
1197+
///
11921198
/// # Examples
11931199
///
11941200
/// ```

library/core/src/result.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,11 @@ impl<T, E> Result<T, E> {
12851285

12861286
/// Returns `res` if the result is [`Ok`], otherwise returns the [`Err`] value of `self`.
12871287
///
1288+
/// Arguments passed to `and` are eagerly evaluated; if you are passing the
1289+
/// result of a function call, it is recommended to use [`and_then`], which is
1290+
/// lazily evaluated.
1291+
///
1292+
/// [`and_then`]: Result::and_then
12881293
///
12891294
/// # Examples
12901295
///

0 commit comments

Comments
 (0)