Skip to content

Commit 70704db

Browse files
committed
Do not lint when range is completely included into another one
1 parent 583715f commit 70704db

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

clippy_lints/src/matches.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,17 @@ where
15441544
}
15451545
},
15461546
(&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (),
1547-
_ => return Some((a.range(), b.range())),
1547+
_ => {
1548+
// skip if the range `a` is completely included into the range `b`
1549+
if let Ordering::Equal | Ordering::Less = a.cmp(&b) {
1550+
let kind_a = Kind::End(a.range().node.1, a.range());
1551+
let kind_b = Kind::End(b.range().node.1, b.range());
1552+
if let Ordering::Equal | Ordering::Greater = kind_a.cmp(&kind_b) {
1553+
return None;
1554+
}
1555+
}
1556+
return Some((a.range(), b.range()));
1557+
},
15481558
}
15491559
}
15501560

tests/ui/match_overlapping_arm.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ fn overlapping() {
5757
_ => (),
5858
}
5959

60+
match 42 {
61+
5..7 => println!("5 .. 7"),
62+
0..10 => println!("0 .. 10"),
63+
_ => (),
64+
}
65+
66+
match 42 {
67+
5..10 => println!("5 .. 10"),
68+
0..=10 => println!("0 ... 10"),
69+
_ => (),
70+
}
71+
6072
/*
6173
// FIXME(JohnTitor): uncomment this once rustfmt knows half-open patterns
6274
match 42 {

tests/ui/match_overlapping_arm.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,6 @@ note: overlaps with this
2323
LL | FOO..=11 => println!("0 ... 11"),
2424
| ^^^^^^^^
2525

26-
error: some ranges overlap
27-
--> $DIR/match_overlapping_arm.rs:26:9
28-
|
29-
LL | 0..=5 => println!("0 ... 5"),
30-
| ^^^^^
31-
|
32-
note: overlaps with this
33-
--> $DIR/match_overlapping_arm.rs:25:9
34-
|
35-
LL | 2 => println!("2"),
36-
| ^
37-
38-
error: some ranges overlap
39-
--> $DIR/match_overlapping_arm.rs:32:9
40-
|
41-
LL | 0..=2 => println!("0 ... 2"),
42-
| ^^^^^
43-
|
44-
note: overlaps with this
45-
--> $DIR/match_overlapping_arm.rs:31:9
46-
|
47-
LL | 2 => println!("2"),
48-
| ^
49-
5026
error: some ranges overlap
5127
--> $DIR/match_overlapping_arm.rs:55:9
5228
|
@@ -59,5 +35,5 @@ note: overlaps with this
5935
LL | 0..=11 => println!("0 ... 11"),
6036
| ^^^^^^
6137

62-
error: aborting due to 5 previous errors
38+
error: aborting due to 3 previous errors
6339

0 commit comments

Comments
 (0)