From 693df63c7d51aca2bb405a0574717a5d02fd8158 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Mon, 1 Nov 2021 06:12:43 +0200 Subject: [PATCH] Ensure `match_overlapping_arms` warns on first --- clippy_lints/src/matches.rs | 13 ++++++++++--- tests/ui/match_overlapping_arm.rs | 9 +++++++++ tests/ui/match_overlapping_arm.stderr | 14 +++++++++++++- 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