about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-08-25 08:50:59 +0900
committerGitHub <noreply@github.com>2022-08-25 08:50:59 +0900
commitdf354f5cf6e0c0a68d7c4d1b1d945182eb7d7811 (patch)
tree58ea75ae736ceff488adf5f81bdedaaddf210e96
parent0fd4a74139347edc1429de3f9d397ec6583698fd (diff)
parenteb2fdd917ef05184c7ef2eab707183b07d68ff5e (diff)
downloadrust-df354f5cf6e0c0a68d7c4d1b1d945182eb7d7811.tar.gz
rust-df354f5cf6e0c0a68d7c4d1b1d945182eb7d7811.zip
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()`](https://github.com/rust-lang/rust/pull/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.
-rw-r--r--library/core/src/option.rs6
-rw-r--r--library/core/src/result.rs5
2 files changed, 11 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index bca73cb770f..93417586363 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -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
     ///
     /// ```
diff --git a/library/core/src/result.rs b/library/core/src/result.rs
index 45b052c824d..75dcb4cdba9 100644
--- a/library/core/src/result.rs
+++ b/library/core/src/result.rs
@@ -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
     ///