Skip to content

Commit c5f3f9d

Browse files
committed
Auto merge of rust-lang#6603 - ThibsG:MatchOverlappingArm5986, r=flip1995
Do not lint when range is completely included into another one This fix has been developed following this [comment](rust-lang/rust-clippy#5986 (comment)). So this will be linted: ``` |----------| |-----------| ``` Now this won't be linted: ``` |---| |--------------------| ``` and this will still lint: ``` |--------| |--------------| ``` Fixes: rust-lang#5986 changelog: Fix FPs in match_overlapping_arm, when first arm is completely included in second arm
2 parents ed11274 + 0518911 commit c5f3f9d

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

clippy_lints/src/matches.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,17 @@ where
15921592
}
15931593
},
15941594
(&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (),
1595-
_ => return Some((a.range(), b.range())),
1595+
_ => {
1596+
// skip if the range `a` is completely included into the range `b`
1597+
if let Ordering::Equal | Ordering::Less = a.cmp(&b) {
1598+
let kind_a = Kind::End(a.range().node.1, a.range());
1599+
let kind_b = Kind::End(b.range().node.1, b.range());
1600+
if let Ordering::Equal | Ordering::Greater = kind_a.cmp(&kind_b) {
1601+
return None;
1602+
}
1603+
}
1604+
return Some((a.range(), b.range()));
1605+
},
15961606
}
15971607
}
15981608

tests/ui/match_overlapping_arm.rs

+30
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ 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+
72+
match 42 {
73+
0..14 => println!("0 .. 14"),
74+
5..10 => println!("5 .. 10"),
75+
_ => (),
76+
}
77+
78+
match 42 {
79+
5..14 => println!("5 .. 14"),
80+
0..=10 => println!("0 ... 10"),
81+
_ => (),
82+
}
83+
84+
match 42 {
85+
0..7 => println!("0 .. 7"),
86+
0..=10 => println!("0 ... 10"),
87+
_ => (),
88+
}
89+
6090
/*
6191
// FIXME(JohnTitor): uncomment this once rustfmt knows half-open patterns
6292
match 42 {

tests/ui/match_overlapping_arm.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,39 @@ LL | FOO..=11 => println!("0 ... 11"),
2424
| ^^^^^^^^
2525

2626
error: some ranges overlap
27-
--> $DIR/match_overlapping_arm.rs:26:9
27+
--> $DIR/match_overlapping_arm.rs:55:9
2828
|
29-
LL | 0..=5 => println!("0 ... 5"),
29+
LL | 0..11 => println!("0 .. 11"),
3030
| ^^^^^
3131
|
3232
note: overlaps with this
33-
--> $DIR/match_overlapping_arm.rs:25:9
33+
--> $DIR/match_overlapping_arm.rs:56:9
3434
|
35-
LL | 2 => println!("2"),
36-
| ^
35+
LL | 0..=11 => println!("0 ... 11"),
36+
| ^^^^^^
3737

3838
error: some ranges overlap
39-
--> $DIR/match_overlapping_arm.rs:32:9
39+
--> $DIR/match_overlapping_arm.rs:80:9
4040
|
41-
LL | 0..=2 => println!("0 ... 2"),
42-
| ^^^^^
41+
LL | 0..=10 => println!("0 ... 10"),
42+
| ^^^^^^
4343
|
4444
note: overlaps with this
45-
--> $DIR/match_overlapping_arm.rs:31:9
45+
--> $DIR/match_overlapping_arm.rs:79:9
4646
|
47-
LL | 2 => println!("2"),
48-
| ^
47+
LL | 5..14 => println!("5 .. 14"),
48+
| ^^^^^
4949

5050
error: some ranges overlap
51-
--> $DIR/match_overlapping_arm.rs:55:9
51+
--> $DIR/match_overlapping_arm.rs:85:9
5252
|
53-
LL | 0..11 => println!("0 .. 11"),
54-
| ^^^^^
53+
LL | 0..7 => println!("0 .. 7"),
54+
| ^^^^
5555
|
5656
note: overlaps with this
57-
--> $DIR/match_overlapping_arm.rs:56:9
57+
--> $DIR/match_overlapping_arm.rs:86:9
5858
|
59-
LL | 0..=11 => println!("0 ... 11"),
59+
LL | 0..=10 => println!("0 ... 10"),
6060
| ^^^^^^
6161

6262
error: aborting due to 5 previous errors

0 commit comments

Comments
 (0)