Skip to content

Commit 6890407

Browse files
committed
Auto merge of rust-lang#118879 - Nadrieril:lint-range-gap, r=estebank
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`.
2 parents 1bd7383 + 2dd085f commit 6890407

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

tests/ui/manual_range_patterns.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unused)]
2+
#![allow(non_contiguous_range_endpoints)]
23
#![warn(clippy::manual_range_patterns)]
34
#![feature(exclusive_range_pattern)]
45

tests/ui/manual_range_patterns.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(unused)]
2+
#![allow(non_contiguous_range_endpoints)]
23
#![warn(clippy::manual_range_patterns)]
34
#![feature(exclusive_range_pattern)]
45

tests/ui/manual_range_patterns.stderr

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this OR pattern can be rewritten using a range
2-
--> tests/ui/manual_range_patterns.rs:8:25
2+
--> tests/ui/manual_range_patterns.rs:9:25
33
|
44
LL | let _ = matches!(f, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
@@ -8,109 +8,109 @@ LL | let _ = matches!(f, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10);
88
= help: to override `-D warnings` add `#[allow(clippy::manual_range_patterns)]`
99

1010
error: this OR pattern can be rewritten using a range
11-
--> tests/ui/manual_range_patterns.rs:9:25
11+
--> tests/ui/manual_range_patterns.rs:10:25
1212
|
1313
LL | let _ = matches!(f, 4 | 2 | 3 | 1 | 5 | 6 | 9 | 7 | 8 | 10);
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
1515

1616
error: this OR pattern can be rewritten using a range
17-
--> tests/ui/manual_range_patterns.rs:16:25
17+
--> tests/ui/manual_range_patterns.rs:17:25
1818
|
1919
LL | let _ = matches!(f, 1 | (2..=4));
2020
| ^^^^^^^^^^^ help: try: `1..=4`
2121

2222
error: this OR pattern can be rewritten using a range
23-
--> tests/ui/manual_range_patterns.rs:17:25
23+
--> tests/ui/manual_range_patterns.rs:18:25
2424
|
2525
LL | let _ = matches!(f, 1 | (2..4));
2626
| ^^^^^^^^^^ help: try: `1..4`
2727

2828
error: this OR pattern can be rewritten using a range
29-
--> tests/ui/manual_range_patterns.rs:18:25
29+
--> tests/ui/manual_range_patterns.rs:19:25
3030
|
3131
LL | let _ = matches!(f, (1..=10) | (2..=13) | (14..=48324728) | 48324729);
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=48324729`
3333

3434
error: this OR pattern can be rewritten using a range
35-
--> tests/ui/manual_range_patterns.rs:19:25
35+
--> tests/ui/manual_range_patterns.rs:20:25
3636
|
3737
LL | let _ = matches!(f, 0 | (1..=10) | 48324730 | (2..=13) | (14..=48324728) | 48324729);
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `0..=48324730`
3939

4040
error: this OR pattern can be rewritten using a range
41-
--> tests/ui/manual_range_patterns.rs:20:25
41+
--> tests/ui/manual_range_patterns.rs:21:25
4242
|
4343
LL | let _ = matches!(f, 0..=1 | 0..=2 | 0..=3);
4444
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `0..=3`
4545

4646
error: this OR pattern can be rewritten using a range
47-
--> tests/ui/manual_range_patterns.rs:23:9
47+
--> tests/ui/manual_range_patterns.rs:24:9
4848
|
4949
LL | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => true,
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
5151

5252
error: this OR pattern can be rewritten using a range
53-
--> tests/ui/manual_range_patterns.rs:26:25
53+
--> tests/ui/manual_range_patterns.rs:27:25
5454
|
5555
LL | let _ = matches!(f, -1 | -5 | 3 | -2 | -4 | -3 | 0 | 1 | 2);
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-5..=3`
5757

5858
error: this OR pattern can be rewritten using a range
59-
--> tests/ui/manual_range_patterns.rs:28:25
59+
--> tests/ui/manual_range_patterns.rs:29:25
6060
|
6161
LL | let _ = matches!(f, -1_000_000..=1_000_000 | -1_000_001 | 1_000_001);
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-1_000_001..=1_000_001`
6363

6464
error: this OR pattern can be rewritten using a range
65-
--> tests/ui/manual_range_patterns.rs:31:17
65+
--> tests/ui/manual_range_patterns.rs:32:17
6666
|
6767
LL | matches!(f, 0x00 | 0x01 | 0x02 | 0x03);
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `0x00..=0x03`
6969

7070
error: this OR pattern can be rewritten using a range
71-
--> tests/ui/manual_range_patterns.rs:32:17
71+
--> tests/ui/manual_range_patterns.rs:33:17
7272
|
7373
LL | matches!(f, 0x00..=0x05 | 0x06 | 0x07);
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `0x00..=0x07`
7575

7676
error: this OR pattern can be rewritten using a range
77-
--> tests/ui/manual_range_patterns.rs:33:17
77+
--> tests/ui/manual_range_patterns.rs:34:17
7878
|
7979
LL | matches!(f, -0x09 | -0x08 | -0x07..=0x00);
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-0x09..=0x00`
8181

8282
error: this OR pattern can be rewritten using a range
83-
--> tests/ui/manual_range_patterns.rs:35:17
83+
--> tests/ui/manual_range_patterns.rs:36:17
8484
|
8585
LL | matches!(f, 0..5 | 5);
8686
| ^^^^^^^^ help: try: `0..=5`
8787

8888
error: this OR pattern can be rewritten using a range
89-
--> tests/ui/manual_range_patterns.rs:36:17
89+
--> tests/ui/manual_range_patterns.rs:37:17
9090
|
9191
LL | matches!(f, 0 | 1..5);
9292
| ^^^^^^^^ help: try: `0..5`
9393

9494
error: this OR pattern can be rewritten using a range
95-
--> tests/ui/manual_range_patterns.rs:38:17
95+
--> tests/ui/manual_range_patterns.rs:39:17
9696
|
9797
LL | matches!(f, 0..=5 | 6..10);
9898
| ^^^^^^^^^^^^^ help: try: `0..10`
9999

100100
error: this OR pattern can be rewritten using a range
101-
--> tests/ui/manual_range_patterns.rs:39:17
101+
--> tests/ui/manual_range_patterns.rs:40:17
102102
|
103103
LL | matches!(f, 0..5 | 5..=10);
104104
| ^^^^^^^^^^^^^ help: try: `0..=10`
105105

106106
error: this OR pattern can be rewritten using a range
107-
--> tests/ui/manual_range_patterns.rs:40:17
107+
--> tests/ui/manual_range_patterns.rs:41:17
108108
|
109109
LL | matches!(f, 5..=10 | 0..5);
110110
| ^^^^^^^^^^^^^ help: try: `0..=10`
111111

112112
error: this OR pattern can be rewritten using a range
113-
--> tests/ui/manual_range_patterns.rs:44:26
113+
--> tests/ui/manual_range_patterns.rs:45:26
114114
|
115115
LL | matches!($e, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10)
116116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`

0 commit comments

Comments
 (0)