forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#118879 - Nadrieril:lint-range-gap, r=<try>
WIP: Lint small gaps between ranges In the discussion to stabilize exclusive range patterns (rust-lang#37854), it has often come up that they're likely to cause off-by-one mistakes. We already have the `overlapping_range_endpoint` lint, so I [proposed](rust-lang#37854 (comment)) the `small_gap_between_ranges` lint as a proof of concept. Here's the idea (see the test file to get a better idea): ```rust match x { 0..10 => ..., // WARN: this range has a small gap on `10_u8`... 11..20 => ..., // ... with this range. _ => ..., } // note: you likely meant to match `10_u8` too ``` For now the PR is meant as illustration so the discussion can progress. I'll make this a real lint if we end up wanting it (and if we can find a good name). r? ghost
- Loading branch information
Showing
8 changed files
with
338 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/ui/pattern/usefulness/integer-ranges/gap_between_ranges.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![feature(exclusive_range_pattern)] | ||
#![deny(overlapping_range_endpoints)] | ||
|
||
macro_rules! m { | ||
($s:expr, $t1:pat, $t2:pat) => { | ||
match $s { | ||
$t1 => {} | ||
$t2 => {} | ||
_ => {} | ||
} | ||
}; | ||
} | ||
|
||
fn main() { | ||
m!(0u8, 20..30, 31..=40); //~ ERROR multiple ranges are one apart | ||
m!(0u8, 31..=40, 20..30); //~ ERROR multiple ranges are one apart | ||
m!(0u8, 20..30, 29..=40); //~ ERROR multiple patterns overlap on their endpoints | ||
m!(0u8, 20..30, 30..=40); | ||
m!(0u8, 20..30, 31..=40); //~ ERROR multiple ranges are one apart | ||
m!(0u8, 20..30, 32..=40); | ||
m!(0u8, 20..30, 31); //~ ERROR multiple ranges are one apart | ||
m!(0u8, 20..30, 31..=32); //~ ERROR multiple ranges are one apart | ||
m!(0u8, 30, 30..=40); | ||
m!(0u8, 30, 31..=40); | ||
m!(0u8, 30, 32..=40); //~ ERROR multiple ranges are one apart | ||
|
||
match 0u8 { | ||
0..10 => {} | ||
10 => {} | ||
11..20 => {} | ||
_ => {} | ||
} | ||
|
||
match 0u8 { | ||
0..10 => {} | ||
21..30 => {} //~ ERROR multiple ranges are one apart | ||
11..20 => {} //~ ERROR multiple ranges are one apart | ||
_ => {} | ||
} | ||
match (0u8, true) { | ||
(0..10, true) => {} | ||
(11..20, true) => {} //~ ERROR multiple ranges are one apart | ||
(11..20, false) => {} //~ ERROR multiple ranges are one apart | ||
_ => {} | ||
} | ||
match (true, 0u8) { | ||
(true, 0..10) => {} | ||
(true, 11..20) => {} //~ ERROR multiple ranges are one apart | ||
(false, 11..20) => {} //~ ERROR multiple ranges are one apart | ||
_ => {} | ||
} | ||
match Some(0u8) { | ||
Some(0..10) => {} | ||
Some(11..20) => {} //~ ERROR multiple ranges are one apart | ||
_ => {} | ||
} | ||
} |
Oops, something went wrong.