]> git.lizzy.rs Git - rust.git/commitdiff
Add a couple small tests to the match-same-arm lint.
authorlaurent <laurent.mazare@gmail.com>
Wed, 29 Nov 2017 20:42:37 +0000 (20:42 +0000)
committerlaurent <laurent.mazare@gmail.com>
Wed, 29 Nov 2017 20:42:37 +0000 (20:42 +0000)
clippy_lints/src/copies.rs
tests/ui/matches.rs
tests/ui/matches.stderr

index 862272456ea413d428064a594e11968baeb26d3b..04874e60224bf98b823387b1d8122b3114419bba 100644 (file)
@@ -203,14 +203,10 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
                     db.span_note(i.body.span, "same as this");
 
                     // Note: this does not use `span_suggestion` on purpose: there is no clean way
-                    // to
-                    // remove the other arm. Building a span and suggest to replace it to "" makes
-                    // an
-                    // even more confusing error message. Also in order not to make up a span for
-                    // the
-                    // whole pattern, the suggestion is only shown when there is only one pattern.
-                    // The
-                    // user should know about `|` if they are already using it…
+                    // to remove the other arm. Building a span and suggest to replace it to ""
+                    // makes an even more confusing error message. Also in order not to make up a
+                    // span for the whole pattern, the suggestion is only shown when there is only
+                    // one pattern. The user should know about `|` if they are already using it…
 
                     if i.pats.len() == 1 && j.pats.len() == 1 {
                         let lhs = snippet(cx, i.pats[0].span, "<pat1>");
index f97038ca1f04f9c4fc8a69da05b5377f1b5071d5..f15a57c4f856f0ec0d234a9b68a0cc483239a608 100644 (file)
@@ -277,6 +277,26 @@ fn match_wild_err_arm() {
         Ok(_) => println!("ok"),
         Err(_) => {unreachable!();}
     }
+
+    // no warning because of the guard
+    match x {
+        Ok(x) if x*x == 64 => println!("ok"),
+        Ok(_) => println!("ok"),
+        Err(_) => println!("err")
+    }
+
+    match (x, Some(1i32)) {
+        (Ok(x), Some(_)) => println!("ok {}", x),
+        (Ok(_), Some(x)) => println!("ok {}", x),
+        _ => println!("err")
+    }
+
+    // no warning because of the different types for x
+    match (x, Some(1.0f64)) {
+        (Ok(x), Some(_)) => println!("ok {}", x),
+        (Ok(_), Some(x)) => println!("ok {}", x),
+        _ => println!("err")
+    }
 }
 
 fn main() {
index bcb94bab26c34db606aa4b24bfff183355d94f86..cc7c5a4fee223cef960caf6949d34dede47ee0c4 100644 (file)
@@ -390,3 +390,21 @@ note: consider refactoring into `Ok(3) | Ok(_)`
     |                  ^^^^^^^^^^^^^^
     = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
 
+error: this `match` has identical arm bodies
+   --> $DIR/matches.rs:290:29
+    |
+290 |         (Ok(_), Some(x)) => println!("ok {}", x),
+    |                             ^^^^^^^^^^^^^^^^^^^^
+    |
+note: same as this
+   --> $DIR/matches.rs:289:29
+    |
+289 |         (Ok(x), Some(_)) => println!("ok {}", x),
+    |                             ^^^^^^^^^^^^^^^^^^^^
+note: consider refactoring into `(Ok(x), Some(_)) | (Ok(_), Some(x))`
+   --> $DIR/matches.rs:289:29
+    |
+289 |         (Ok(x), Some(_)) => println!("ok {}", x),
+    |                             ^^^^^^^^^^^^^^^^^^^^
+    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
+