about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Wright <mikerite@lavabit.com>2021-11-01 06:12:43 +0200
committerMichael Wright <mikerite@lavabit.com>2021-11-01 06:12:43 +0200
commit693df63c7d51aca2bb405a0574717a5d02fd8158 (patch)
tree41cdea30523719dd4358347d0f15eaafbbdfd977
parentc3d45775c438c57450317ccc859d3af10ce08972 (diff)
downloadrust-693df63c7d51aca2bb405a0574717a5d02fd8158.tar.gz
rust-693df63c7d51aca2bb405a0574717a5d02fd8158.zip
Ensure `match_overlapping_arms` warns on first
-rw-r--r--clippy_lints/src/matches.rs13
-rw-r--r--tests/ui/match_overlapping_arm.rs9
-rw-r--r--tests/ui/match_overlapping_arm.stderr14
3 files changed, 32 insertions, 4 deletions
diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs
index 3511defc59c..b552e3fc7af 100644
--- a/clippy_lints/src/matches.rs
+++ b/clippy_lints/src/matches.rs
@@ -1761,10 +1761,17 @@ where
         match value {
             Kind::Start(_, r) => started.push(r),
             Kind::End(_, er) => {
-                if let Some(sr) = started.pop() {
-                    if sr != er {
-                        return Some((er, sr));
+                let mut overlap = None;
+
+                while let Some(sr) = started.pop() {
+                    if sr == er {
+                        break;
                     }
+                    overlap = Some(sr);
+                }
+
+                if let Some(sr) = overlap {
+                    return Some((er, sr));
                 }
             },
         }
diff --git a/tests/ui/match_overlapping_arm.rs b/tests/ui/match_overlapping_arm.rs
index c25f59c62a5..2f85e635713 100644
--- a/tests/ui/match_overlapping_arm.rs
+++ b/tests/ui/match_overlapping_arm.rs
@@ -116,6 +116,15 @@ fn overlapping() {
         _ => (),
     }
 
+    // Only warn about the first if there are multiple overlaps
+    match 42u128 {
+        0..=0x0000_0000_0000_00ff => (),
+        0..=0x0000_0000_0000_ffff => (),
+        0..=0x0000_0000_ffff_ffff => (),
+        0..=0xffff_ffff_ffff_ffff => (),
+        _ => (),
+    }
+
     if let None = Some(42) {
         // nothing
     } else if let None = Some(42) {
diff --git a/tests/ui/match_overlapping_arm.stderr b/tests/ui/match_overlapping_arm.stderr
index 69f794edb6a..b81bb1ecfae 100644
--- a/tests/ui/match_overlapping_arm.stderr
+++ b/tests/ui/match_overlapping_arm.stderr
@@ -83,5 +83,17 @@ note: overlaps with this
 LL |         21..=40 => (),
    |         ^^^^^^^
 
-error: aborting due to 7 previous errors
+error: some ranges overlap
+  --> $DIR/match_overlapping_arm.rs:121:9
+   |
+LL |         0..=0x0000_0000_0000_00ff => (),
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+note: overlaps with this
+  --> $DIR/match_overlapping_arm.rs:122:9
+   |
+LL |         0..=0x0000_0000_0000_ffff => (),
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 8 previous errors