Skip to content

Commit

Permalink
Do not lint when range is completely included into another one
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Jan 17, 2021
1 parent 583715f commit 70704db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
12 changes: 11 additions & 1 deletion clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
},
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/match_overlapping_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 1 addition & 25 deletions tests/ui/match_overlapping_arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@ note: overlaps with this
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
|
Expand All @@ -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

0 comments on commit 70704db

Please sign in to comment.