Skip to content

Commit

Permalink
Ensure match_overlapping_arms warns on first
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wright committed Nov 1, 2021
1 parent c3d4577 commit 693df63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 10 additions & 3 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
},
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/match_overlapping_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/match_overlapping_arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 693df63

Please sign in to comment.