Skip to content

Commit

Permalink
Auto merge of #118879 - Nadrieril:lint-range-gap, r=estebank
Browse files Browse the repository at this point in the history
Lint singleton gaps after exclusive ranges

In the discussion to stabilize exclusive range patterns (rust-lang/rust#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/rust#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`.
  • Loading branch information
bors committed Mar 9, 2024
2 parents 6a604cc + cd1f334 commit 0514f44
Showing 0 changed files with 0 additions and 0 deletions.

0 comments on commit 0514f44

Please sign in to comment.