about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-03-04 18:23:39 +0000
committerbors <bors@rust-lang.org>2021-03-04 18:23:39 +0000
commit7be3a32f25f514fa376cfbdfd6e89d77d0dd350e (patch)
tree5afe82864bfe2ec03335611aa22b79e95553bb08 /tests
parent66807109613a233eb867651b32c4d36bee541c2c (diff)
parent39c5e863378270c25ef75f3ae53d908565bc2619 (diff)
downloadrust-7be3a32f25f514fa376cfbdfd6e89d77d0dd350e.tar.gz
rust-7be3a32f25f514fa376cfbdfd6e89d77d0dd350e.zip
Auto merge of #6843 - Jarcho:match_on_same_arms_macro, r=Manishearth
Compare empty blocks for equality based on tokens

fixes: #1390

This only considers empty blocks for now, though we should also catch something like this:

```rust
match 0 {
    0 => {
        do_something();
        trace!(0);
        0
    }
    1 => {
        do_something();
        trace!(1);
        1
    }
    x => x,
}
```

As far as I can tell there aren't any negative effects on other lints. These blocks only happen to be the same for a given compilation, not all compilations.

changelog: Fix `match_on_same_arms` and others. Only consider empty blocks equal if the tokens contained are the same.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/match_same_arms2.rs29
-rw-r--r--tests/ui/match_same_arms2.stderr27
2 files changed, 54 insertions, 2 deletions
diff --git a/tests/ui/match_same_arms2.rs b/tests/ui/match_same_arms2.rs
index 06d91497242..da4e3020d5b 100644
--- a/tests/ui/match_same_arms2.rs
+++ b/tests/ui/match_same_arms2.rs
@@ -120,6 +120,35 @@ fn match_same_arms() {
         },
     }
 
+    // False positive #1390
+    macro_rules! empty {
+        ($e:expr) => {};
+    }
+    match 0 {
+        0 => {
+            empty!(0);
+        },
+        1 => {
+            empty!(1);
+        },
+        x => {
+            empty!(x);
+        },
+    };
+
+    // still lint if the tokens are the same
+    match 0 {
+        0 => {
+            empty!(0);
+        },
+        1 => {
+            empty!(0);
+        },
+        x => {
+            empty!(x);
+        },
+    }
+
     match_expr_like_matches_macro_priority();
 }
 
diff --git a/tests/ui/match_same_arms2.stderr b/tests/ui/match_same_arms2.stderr
index fccaf805616..95f9494cdc9 100644
--- a/tests/ui/match_same_arms2.stderr
+++ b/tests/ui/match_same_arms2.stderr
@@ -141,8 +141,31 @@ LL |         Ok(3) => println!("ok"),
    |         ^^^^^
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
 
+error: this `match` has identical arm bodies
+  --> $DIR/match_same_arms2.rs:144:14
+   |
+LL |           1 => {
+   |  ______________^
+LL | |             empty!(0);
+LL | |         },
+   | |_________^
+   |
+note: same as this
+  --> $DIR/match_same_arms2.rs:141:14
+   |
+LL |           0 => {
+   |  ______________^
+LL | |             empty!(0);
+LL | |         },
+   | |_________^
+help: consider refactoring into `0 | 1`
+  --> $DIR/match_same_arms2.rs:141:9
+   |
+LL |         0 => {
+   |         ^
+
 error: match expression looks like `matches!` macro
-  --> $DIR/match_same_arms2.rs:133:16
+  --> $DIR/match_same_arms2.rs:162:16
    |
 LL |       let _ans = match x {
    |  ________________^
@@ -154,5 +177,5 @@ LL | |     };
    |
    = note: `-D clippy::match-like-matches-macro` implied by `-D warnings`
 
-error: aborting due to 8 previous errors
+error: aborting due to 9 previous errors