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
Lint singleton gaps after exclusive ranges #118879
Conversation
Let's see if this makes @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
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
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
89053d1
to
4992810
Compare
This comment has been minimized.
This comment has been minimized.
4992810
to
66e644c
Compare
This comment has been minimized.
This comment has been minimized.
This comment was marked as outdated.
This comment was marked as outdated.
66e644c
to
f538ce7
Compare
This comment has been minimized.
This comment has been minimized.
tests/ui/pattern/usefulness/integer-ranges/gap_between_ranges.stderr
Outdated
Show resolved
Hide resolved
tests/ui/pattern/usefulness/integer-ranges/gap_between_ranges.stderr
Outdated
Show resolved
Hide resolved
tests/ui/pattern/usefulness/integer-ranges/gap_between_ranges.stderr
Outdated
Show resolved
Hide resolved
f538ce7
to
3ef781c
Compare
This comment has been minimized.
This comment has been minimized.
3ef781c
to
ba60bce
Compare
This comment has been minimized.
This comment has been minimized.
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Lint singleton gaps after exclusive 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_endpoints` lint, so I [proposed](rust-lang#37854 (comment)) a lint to catch the complementary mistake. This PR adds a new `non_contiguous_range_endpoints` lint that catches likely off-by-one errors with exclusive range patterns. Here's the idea (see the test file for more examples): ```rust match x { 0..10 => ..., // WARN: this range doesn't match `10_u8` because `..` is an exclusive range 11..20 => ..., // this could appear to continue range `0_u8..10_u8`, but `10_u8` isn't matched by either of them _ => ..., } // help: use an inclusive range instead: `0_u8..=10_u8` ``` More precisely: for any exclusive range `lo..hi`, if `hi+1` is matched by another range but `hi` isn't, we suggest writing an inclusive range `lo..=hi` instead. We also catch `lo..T::MAX`.
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
afb36a1
to
8ac9a04
Compare
☀️ Test successful - checks-actions |
Finished benchmarking commit (1b427b3): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 650.25s -> 650.027s (-0.03%) |
Lint singleton gaps after exclusive 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_endpoints` lint, so I [proposed](rust-lang#37854 (comment)) a lint to catch the complementary mistake. This PR adds a new `non_contiguous_range_endpoints` lint that catches likely off-by-one errors with exclusive range patterns. Here's the idea (see the test file for more examples): ```rust match x { 0..10 => ..., // WARN: this range doesn't match `10_u8` because `..` is an exclusive range 11..20 => ..., // this could appear to continue range `0_u8..10_u8`, but `10_u8` isn't matched by either of them _ => ..., } // help: use an inclusive range instead: `0_u8..=10_u8` ``` More precisely: for any exclusive range `lo..hi`, if `hi+1` is matched by another range but `hi` isn't, we suggest writing an inclusive range `lo..=hi` instead. We also catch `lo..T::MAX`.
In the discussion to stabilize exclusive range patterns (#37854), it has often come up that they're likely to cause off-by-one mistakes. We already have the
overlapping_range_endpoints
lint, so I proposed a lint to catch the complementary mistake.This PR adds a new
non_contiguous_range_endpoints
lint that catches likely off-by-one errors with exclusive range patterns. Here's the idea (see the test file for more examples):More precisely: for any exclusive range
lo..hi
, ifhi+1
is matched by another range buthi
isn't, we suggest writing an inclusive rangelo..=hi
instead. We also catchlo..T::MAX
.