]> git.lizzy.rs Git - rust.git/commitdiff
manual_unwrap_or / use consts::constant_simple helper
authorTim Nielens <tim.nielens@gmail.com>
Sun, 11 Oct 2020 22:06:21 +0000 (00:06 +0200)
committerTim Nielens <tim.nielens@gmail.com>
Wed, 14 Oct 2020 20:16:48 +0000 (22:16 +0200)
clippy_lints/src/manual_unwrap_or.rs
tests/ui/manual_unwrap_or.fixed
tests/ui/manual_unwrap_or.stderr

index 9d8fc863424c96d52fe5259911c3076e8f521ef4..ced941fac1a48b7520d26d6f170e20854cf44a30 100644 (file)
@@ -1,3 +1,4 @@
+use crate::consts::constant_simple;
 use crate::utils;
 use if_chain::if_chain;
 use rustc_errors::Applicability;
     ///
     /// **Example:**
     /// ```rust
-    /// match int_option {
+    /// let foo: Option<i32> = None;
+    /// match foo {
     ///     Some(v) => v,
     ///     None => 1,
-    /// }
+    /// };
     /// ```
     ///
     /// Use instead:
     /// ```rust
-    /// int_option.unwrap_or(1)
+    /// let foo: Option<i32> = None;
+    /// foo.unwrap_or(1);
     /// ```
     pub MANUAL_UNWRAP_OR,
     complexity,
@@ -84,7 +87,7 @@ fn applicable_none_arm<'a>(arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
         then {
             let reindented_none_body =
                 utils::reindent_multiline(none_body_snippet.into(), true, Some(indent));
-            let eager_eval = utils::eager_or_lazy::is_eagerness_candidate(cx, none_arm.body);
+            let eager_eval = constant_simple(cx, cx.typeck_results(), none_arm.body).is_some();
             let method = if eager_eval {
                 "unwrap_or"
             } else {
index 99d30360db1a1bef807d4bbdf7b25b2b77bcfa7b..a9cc8678c9d17e25d71e541f40893762a3d25190 100644 (file)
@@ -9,7 +9,7 @@ fn unwrap_or() {
     Some(1).unwrap_or(42);
 
     // richer none expr
-    Some(1).unwrap_or_else(|| 1 + 42);
+    Some(1).unwrap_or(1 + 42);
 
     // multiline case
     Some(1).unwrap_or_else(|| {
index 03da118a0c420dd78140ec31fc0f9303999f0869..8f6835ed78d290002a7f7d3e335196d3d828a6c2 100644 (file)
@@ -18,14 +18,14 @@ LL | |         Some(i) => i,
 LL | |     };
    | |_____^ help: replace with: `Some(1).unwrap_or(42)`
 
-error: this pattern reimplements `Option::unwrap_or_else`
+error: this pattern reimplements `Option::unwrap_or`
   --> $DIR/manual_unwrap_or.rs:18:5
    |
 LL | /     match Some(1) {
 LL | |         Some(i) => i,
 LL | |         None => 1 + 42,
 LL | |     };
-   | |_____^ help: replace with: `Some(1).unwrap_or_else(|| 1 + 42)`
+   | |_____^ help: replace with: `Some(1).unwrap_or(1 + 42)`
 
 error: this pattern reimplements `Option::unwrap_or_else`
   --> $DIR/manual_unwrap_or.rs:24:5