diff options
| author | ThibsG <Thibs@debian.com> | 2021-01-15 18:51:00 +0100 |
|---|---|---|
| committer | ThibsG <Thibs@debian.com> | 2021-01-17 21:07:01 +0100 |
| commit | 70704db36fd07353de58c9330397d0137afdeebe (patch) | |
| tree | 9c639e2dfe5bb8b3fdd68d468e6cad22ef17c41c | |
| parent | 583715f583bd28ec759a5d0737b4ec400f0814cd (diff) | |
| download | rust-70704db36fd07353de58c9330397d0137afdeebe.tar.gz rust-70704db36fd07353de58c9330397d0137afdeebe.zip | |
Do not lint when range is completely included into another one
| -rw-r--r-- | clippy_lints/src/matches.rs | 12 | ||||
| -rw-r--r-- | tests/ui/match_overlapping_arm.rs | 12 | ||||
| -rw-r--r-- | tests/ui/match_overlapping_arm.stderr | 26 |
3 files changed, 24 insertions, 26 deletions
diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 04b35835c6b..90c63f32c13 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -1544,7 +1544,17 @@ where } }, (&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (), - _ => return Some((a.range(), b.range())), + _ => { + // skip if the range `a` is completely included into the range `b` + if let Ordering::Equal | Ordering::Less = a.cmp(&b) { + let kind_a = Kind::End(a.range().node.1, a.range()); + let kind_b = Kind::End(b.range().node.1, b.range()); + if let Ordering::Equal | Ordering::Greater = kind_a.cmp(&kind_b) { + return None; + } + } + return Some((a.range(), b.range())); + }, } } diff --git a/tests/ui/match_overlapping_arm.rs b/tests/ui/match_overlapping_arm.rs index 97789bb766f..3e40f2187bf 100644 --- a/tests/ui/match_overlapping_arm.rs +++ b/tests/ui/match_overlapping_arm.rs @@ -57,6 +57,18 @@ fn overlapping() { _ => (), } + match 42 { + 5..7 => println!("5 .. 7"), + 0..10 => println!("0 .. 10"), + _ => (), + } + + match 42 { + 5..10 => println!("5 .. 10"), + 0..=10 => println!("0 ... 10"), + _ => (), + } + /* // FIXME(JohnTitor): uncomment this once rustfmt knows half-open patterns match 42 { diff --git a/tests/ui/match_overlapping_arm.stderr b/tests/ui/match_overlapping_arm.stderr index eb20d5405a9..74259cd88c7 100644 --- a/tests/ui/match_overlapping_arm.stderr +++ b/tests/ui/match_overlapping_arm.stderr @@ -24,30 +24,6 @@ LL | FOO..=11 => println!("0 ... 11"), | ^^^^^^^^ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:26:9 - | -LL | 0..=5 => println!("0 ... 5"), - | ^^^^^ - | -note: overlaps with this - --> $DIR/match_overlapping_arm.rs:25:9 - | -LL | 2 => println!("2"), - | ^ - -error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:32:9 - | -LL | 0..=2 => println!("0 ... 2"), - | ^^^^^ - | -note: overlaps with this - --> $DIR/match_overlapping_arm.rs:31:9 - | -LL | 2 => println!("2"), - | ^ - -error: some ranges overlap --> $DIR/match_overlapping_arm.rs:55:9 | LL | 0..11 => println!("0 .. 11"), @@ -59,5 +35,5 @@ note: overlaps with this LL | 0..=11 => println!("0 ... 11"), | ^^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 3 previous errors |
