Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not lint when range is completely included into another one #6603

Merged
merged 2 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions tests/ui/match_overlapping_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ 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"),
_ => (),
}
ThibsG marked this conversation as resolved.
Show resolved Hide resolved

match 42 {
0..14 => println!("0 .. 14"),
5..10 => println!("5 .. 10"),
_ => (),
}
Comment on lines +72 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, shouldn't rustc trigger here? the second arm is unreachable. Well, at least this lint doesn't trigger here.

A quick check on Playground shows that rustc triggers here correctly. So I assume this is a late pass lint in rustc and therefore would only trigger once the clippy lint doesn't trigger anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I was a bit surprise too, I was also waiting for an unreachable warning 😅


match 42 {
5..14 => println!("5 .. 14"),
0..=10 => println!("0 ... 10"),
_ => (),
}

match 42 {
0..7 => println!("0 .. 7"),
0..=10 => println!("0 ... 10"),
_ => (),
}

/*
// FIXME(JohnTitor): uncomment this once rustfmt knows half-open patterns
match 42 {
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/match_overlapping_arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@ LL | FOO..=11 => println!("0 ... 11"),
| ^^^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:26:9
--> $DIR/match_overlapping_arm.rs:55:9
|
LL | 0..=5 => println!("0 ... 5"),
LL | 0..11 => println!("0 .. 11"),
| ^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:25:9
--> $DIR/match_overlapping_arm.rs:56:9
|
LL | 2 => println!("2"),
| ^
LL | 0..=11 => println!("0 ... 11"),
| ^^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:32:9
--> $DIR/match_overlapping_arm.rs:80:9
|
LL | 0..=2 => println!("0 ... 2"),
| ^^^^^
LL | 0..=10 => println!("0 ... 10"),
| ^^^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:31:9
--> $DIR/match_overlapping_arm.rs:79:9
|
LL | 2 => println!("2"),
| ^
LL | 5..14 => println!("5 .. 14"),
| ^^^^^

error: some ranges overlap
--> $DIR/match_overlapping_arm.rs:55:9
--> $DIR/match_overlapping_arm.rs:85:9
|
LL | 0..11 => println!("0 .. 11"),
| ^^^^^
LL | 0..7 => println!("0 .. 7"),
| ^^^^
|
note: overlaps with this
--> $DIR/match_overlapping_arm.rs:56:9
--> $DIR/match_overlapping_arm.rs:86:9
|
LL | 0..=11 => println!("0 ... 11"),
LL | 0..=10 => println!("0 ... 10"),
| ^^^^^^

error: aborting due to 5 previous errors
Expand Down