]> git.lizzy.rs Git - rust.git/commitdiff
manual-unwrap-or / pr remarks, round 2
authorTim Nielens <tim.nielens@gmail.com>
Thu, 22 Oct 2020 21:39:59 +0000 (23:39 +0200)
committerTim Nielens <tim.nielens@gmail.com>
Thu, 22 Oct 2020 21:39:59 +0000 (23:39 +0200)
clippy_lints/src/manual_unwrap_or.rs
tests/ui/manual_unwrap_or.fixed
tests/ui/manual_unwrap_or.rs
tests/ui/manual_unwrap_or.stderr

index f3f1e31abde73829e7a9179d49ffaaa8e7ce1396..cc9ee28d0275db84f5041f4c1667f5088cd454fe 100644 (file)
@@ -111,7 +111,9 @@ fn applicable_or_arm<'a>(arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
         then {
             let reindented_or_body =
                 utils::reindent_multiline(or_body_snippet.into(), true, Some(indent));
-            let wrap_in_parens = !matches!(scrutinee, Expr { kind: ExprKind::Call(..), .. });
+            let wrap_in_parens = !matches!(scrutinee, Expr {
+                kind: ExprKind::Call(..) | ExprKind::Path(_), ..
+            });
             let l_paren = if wrap_in_parens { "(" } else { "" };
             let r_paren = if wrap_in_parens { ")" } else { "" };
             utils::span_lint_and_sugg(
index c784de0f604751a5557fa0e40e17470305b4d10f..582f5c6f7a8eece60d2f7814726ffa10b4c319e4 100644 (file)
@@ -70,6 +70,10 @@ fn result_unwrap_or() {
     // int case
     Ok::<i32, &str>(1).unwrap_or(42);
 
+    // int case, scrutinee is a binding
+    let a = Ok::<i32, &str>(1);
+    a.unwrap_or(42);
+
     // int case, suggestion must surround with parenthesis
     (Ok(1) as Result<i32, &str>).unwrap_or(42);
 
index df5f237c3fbaf1ba591095e6300a0b904c85f227..0e2b7ecadb312c5cf3cd83c08007a8999697f1e0 100644 (file)
@@ -88,6 +88,13 @@ fn result_unwrap_or() {
         Err(_) => 42,
     };
 
+    // int case, scrutinee is a binding
+    let a = Ok::<i32, &str>(1);
+    match a {
+        Ok(i) => i,
+        Err(_) => 42,
+    };
+
     // int case, suggestion must surround with parenthesis
     match Ok(1) as Result<i32, &str> {
         Ok(i) => i,
index 5bc01bf4e68e162ba4a45ce2f5596d3e24c35160..6603ab43437d48412475eabfe5c18a4e42faef06 100644 (file)
@@ -67,7 +67,16 @@ LL | |     };
    | |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
 
 error: this pattern reimplements `Result::unwrap_or`
-  --> $DIR/manual_unwrap_or.rs:92:5
+  --> $DIR/manual_unwrap_or.rs:93:5
+   |
+LL | /     match a {
+LL | |         Ok(i) => i,
+LL | |         Err(_) => 42,
+LL | |     };
+   | |_____^ help: replace with: `a.unwrap_or(42)`
+
+error: this pattern reimplements `Result::unwrap_or`
+  --> $DIR/manual_unwrap_or.rs:99:5
    |
 LL | /     match Ok(1) as Result<i32, &str> {
 LL | |         Ok(i) => i,
@@ -76,7 +85,7 @@ LL | |     };
    | |_____^ help: replace with: `(Ok(1) as Result<i32, &str>).unwrap_or(42)`
 
 error: this pattern reimplements `Result::unwrap_or`
-  --> $DIR/manual_unwrap_or.rs:98:5
+  --> $DIR/manual_unwrap_or.rs:105:5
    |
 LL | /     match Ok::<i32, &str>(1) {
 LL | |         Err(_) => 42,
@@ -85,7 +94,7 @@ LL | |     };
    | |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(42)`
 
 error: this pattern reimplements `Result::unwrap_or`
-  --> $DIR/manual_unwrap_or.rs:104:5
+  --> $DIR/manual_unwrap_or.rs:111:5
    |
 LL | /     match Ok::<i32, &str>(1) {
 LL | |         Ok(i) => i,
@@ -94,7 +103,7 @@ LL | |     };
    | |_____^ help: replace with: `Ok::<i32, &str>(1).unwrap_or(1 + 42)`
 
 error: this pattern reimplements `Result::unwrap_or`
-  --> $DIR/manual_unwrap_or.rs:111:5
+  --> $DIR/manual_unwrap_or.rs:118:5
    |
 LL | /     match Ok::<i32, &str>(1) {
 LL | |         Ok(i) => i,
@@ -115,7 +124,7 @@ LL |     });
    |
 
 error: this pattern reimplements `Result::unwrap_or`
-  --> $DIR/manual_unwrap_or.rs:121:5
+  --> $DIR/manual_unwrap_or.rs:128:5
    |
 LL | /     match Ok::<&str, &str>("Bob") {
 LL | |         Ok(i) => i,
@@ -123,5 +132,5 @@ LL | |         Err(_) => "Alice",
 LL | |     };
    | |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
 
-error: aborting due to 11 previous errors
+error: aborting due to 12 previous errors