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

Lint singleton gaps after exclusive ranges #118879

Merged
merged 4 commits into from Mar 9, 2024

Conversation

Nadrieril
Copy link
Contributor

@Nadrieril Nadrieril commented Dec 12, 2023

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):

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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 12, 2023
@Nadrieril
Copy link
Contributor Author

Nadrieril commented Dec 12, 2023

Let's see if this makes unicode_normalization sad.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 12, 2023
@bors
Copy link
Contributor

bors commented Dec 12, 2023

⌛ Trying commit 89053d1 with merge 8b07d6f...

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 12, 2023
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
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 12, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as outdated.

@Nadrieril Nadrieril added the A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns label Dec 19, 2023
@Nadrieril Nadrieril added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 19, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rfcbot
Copy link

rfcbot commented Mar 8, 2024

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.

@Nadrieril
Copy link
Contributor Author

@bors r=@estebank

@bors
Copy link
Contributor

bors commented Mar 8, 2024

📌 Commit afb36a1 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Mar 8, 2024
@Nadrieril Nadrieril removed the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Mar 8, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 8, 2024
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`.
@bors
Copy link
Contributor

bors commented Mar 8, 2024

⌛ Testing commit afb36a1 with merge e54568e...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 8, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 8, 2024
@Nadrieril
Copy link
Contributor Author

@bors r=@estebank

@bors
Copy link
Contributor

bors commented Mar 9, 2024

📌 Commit 8ac9a04 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 9, 2024
@bors
Copy link
Contributor

bors commented Mar 9, 2024

⌛ Testing commit 8ac9a04 with merge 1b427b3...

@bors
Copy link
Contributor

bors commented Mar 9, 2024

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 1b427b3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 9, 2024
@bors bors merged commit 1b427b3 into rust-lang:master Mar 9, 2024
12 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 9, 2024
@Nadrieril Nadrieril deleted the lint-range-gap branch March 9, 2024 10:53
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1b427b3): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
2.5% [1.6%, 5.2%] 5
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [-0.6%, 2.9%] 2

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 650.25s -> 650.027s (-0.03%)
Artifact size: 172.57 MiB -> 172.57 MiB (-0.00%)

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Mar 14, 2024
matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2024
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-exhaustiveness-checking Relating to exhaustiveness / usefulness checking of patterns disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. F-exclusive_range_pattern `#![feature(exclusive_range_pattern)]` finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet