Skip to content

Commit 5bce850

Browse files
authored
Rollup merge of rust-lang#78242 - Nadrieril:rename-overlapping_endpoints-lint, r=varkor
Rename `overlapping_patterns` lint As discussed in rust-lang#65477. I also tweaked a few things along the way. r? ```@varkor``` ```@rustbot``` modify labels: +A-exhaustiveness-checking
2 parents 11c94a1 + 5b6c175 commit 5bce850

File tree

11 files changed

+123
-107
lines changed

11 files changed

+123
-107
lines changed

compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
283283
UNUSED_MUT,
284284
UNREACHABLE_CODE,
285285
UNREACHABLE_PATTERNS,
286-
OVERLAPPING_PATTERNS,
287286
UNUSED_MUST_USE,
288287
UNUSED_UNSAFE,
289288
PATH_STATEMENTS,
@@ -335,6 +334,7 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
335334
store.register_renamed("exceeding_bitshifts", "arithmetic_overflow");
336335
store.register_renamed("redundant_semicolon", "redundant_semicolons");
337336
store.register_renamed("intra_doc_link_resolution_failure", "broken_intra_doc_links");
337+
store.register_renamed("overlapping_patterns", "overlapping_range_endpoints");
338338
store.register_removed("unknown_features", "replaced by an error");
339339
store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
340340
store.register_removed("negate_unsigned", "cast a signed value instead");

compiler/rustc_lint_defs/src/builtin.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,8 @@ declare_lint! {
588588
}
589589

590590
declare_lint! {
591-
/// The `overlapping_patterns` lint detects `match` arms that have
592-
/// [range patterns] that overlap.
591+
/// The `overlapping_range_endpoints` lint detects `match` arms that have [range patterns] that
592+
/// overlap on their endpoints.
593593
///
594594
/// [range patterns]: https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns
595595
///
@@ -607,13 +607,12 @@ declare_lint! {
607607
///
608608
/// ### Explanation
609609
///
610-
/// It is likely a mistake to have range patterns in a match expression
611-
/// that overlap. Check that the beginning and end values are what you
612-
/// expect, and keep in mind that with `..=` the left and right bounds are
613-
/// inclusive.
614-
pub OVERLAPPING_PATTERNS,
610+
/// It is likely a mistake to have range patterns in a match expression that overlap in this
611+
/// way. Check that the beginning and end values are what you expect, and keep in mind that
612+
/// with `..=` the left and right bounds are inclusive.
613+
pub OVERLAPPING_RANGE_ENDPOINTS,
615614
Warn,
616-
"detects overlapping patterns"
615+
"detects range patterns with overlapping endpoints"
617616
}
618617

619618
declare_lint! {
@@ -2809,7 +2808,7 @@ declare_lint_pass! {
28092808
DEAD_CODE,
28102809
UNREACHABLE_CODE,
28112810
UNREACHABLE_PATTERNS,
2812-
OVERLAPPING_PATTERNS,
2811+
OVERLAPPING_RANGE_ENDPOINTS,
28132812
BINDINGS_WITH_VARIANT_NAME,
28142813
UNUSED_MACROS,
28152814
WARNINGS,

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl IntRange {
161161
// 2 -------- // 2 -------
162162
let (lo, hi) = self.boundaries();
163163
let (other_lo, other_hi) = other.boundaries();
164-
lo == other_hi || hi == other_lo
164+
(lo == other_hi || hi == other_lo) && !self.is_singleton() && !other.is_singleton()
165165
}
166166

167167
fn to_pat<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Pat<'tcx> {
@@ -273,7 +273,7 @@ impl IntRange {
273273
let mut borders: Vec<_> = row_borders.chain(self_borders).collect();
274274
borders.sort_unstable();
275275

276-
self.lint_overlapping_patterns(pcx, hir_id, overlaps);
276+
self.lint_overlapping_range_endpoints(pcx, hir_id, overlaps);
277277

278278
// We're going to iterate through every adjacent pair of borders, making sure that
279279
// each represents an interval of nonnegative length, and convert each such
@@ -296,30 +296,30 @@ impl IntRange {
296296
.collect()
297297
}
298298

299-
fn lint_overlapping_patterns(
299+
fn lint_overlapping_range_endpoints(
300300
&self,
301301
pcx: PatCtxt<'_, '_, '_>,
302302
hir_id: Option<HirId>,
303303
overlaps: Vec<(IntRange, Span)>,
304304
) {
305305
if let (true, Some(hir_id)) = (!overlaps.is_empty(), hir_id) {
306306
pcx.cx.tcx.struct_span_lint_hir(
307-
lint::builtin::OVERLAPPING_PATTERNS,
307+
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
308308
hir_id,
309309
pcx.span,
310310
|lint| {
311-
let mut err = lint.build("multiple patterns covering the same range");
312-
err.span_label(pcx.span, "overlapping patterns");
311+
let mut err = lint.build("multiple patterns overlap on their endpoints");
313312
for (int_range, span) in overlaps {
314-
// Use the real type for user display of the ranges:
315313
err.span_label(
316314
span,
317315
&format!(
318-
"this range overlaps on `{}`",
319-
int_range.to_pat(pcx.cx.tcx, pcx.ty),
316+
"this range overlaps on `{}`...",
317+
int_range.to_pat(pcx.cx.tcx, pcx.ty)
320318
),
321319
);
322320
}
321+
err.span_label(pcx.span, "... with this range");
322+
err.note("you likely meant to write mutually exclusive ranges");
323323
err.emit();
324324
},
325325
);

src/test/ui/issues/issue-21475.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-pass
2-
#![allow(unused_imports, overlapping_patterns)]
2+
#![allow(unused_imports, overlapping_range_endpoints)]
33
// pretty-expanded FIXME #23616
44

55
use m::{START, END};

src/test/ui/issues/issue-26251.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-pass
2-
#![allow(overlapping_patterns)]
2+
#![allow(overlapping_range_endpoints)]
33

44
fn main() {
55
let x = 'a';

src/test/ui/pattern/usefulness/integer-ranges/exhaustiveness.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(exclusive_range_pattern)]
22
#![feature(assoc_char_consts)]
3+
#![allow(overlapping_range_endpoints)]
34
#![deny(unreachable_patterns)]
45

56
macro_rules! m {

src/test/ui/pattern/usefulness/integer-ranges/exhaustiveness.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0004]: non-exhaustive patterns: `u8::MAX` not covered
2-
--> $DIR/exhaustiveness.rs:47:8
2+
--> $DIR/exhaustiveness.rs:48:8
33
|
44
LL | m!(0u8, 0..255);
55
| ^^^ pattern `u8::MAX` not covered
@@ -8,7 +8,7 @@ LL | m!(0u8, 0..255);
88
= note: the matched value is of type `u8`
99

1010
error[E0004]: non-exhaustive patterns: `u8::MAX` not covered
11-
--> $DIR/exhaustiveness.rs:48:8
11+
--> $DIR/exhaustiveness.rs:49:8
1212
|
1313
LL | m!(0u8, 0..=254);
1414
| ^^^ pattern `u8::MAX` not covered
@@ -17,7 +17,7 @@ LL | m!(0u8, 0..=254);
1717
= note: the matched value is of type `u8`
1818

1919
error[E0004]: non-exhaustive patterns: `0_u8` not covered
20-
--> $DIR/exhaustiveness.rs:49:8
20+
--> $DIR/exhaustiveness.rs:50:8
2121
|
2222
LL | m!(0u8, 1..=255);
2323
| ^^^ pattern `0_u8` not covered
@@ -26,7 +26,7 @@ LL | m!(0u8, 1..=255);
2626
= note: the matched value is of type `u8`
2727

2828
error[E0004]: non-exhaustive patterns: `42_u8` not covered
29-
--> $DIR/exhaustiveness.rs:50:8
29+
--> $DIR/exhaustiveness.rs:51:8
3030
|
3131
LL | m!(0u8, 0..42 | 43..=255);
3232
| ^^^ pattern `42_u8` not covered
@@ -35,7 +35,7 @@ LL | m!(0u8, 0..42 | 43..=255);
3535
= note: the matched value is of type `u8`
3636

3737
error[E0004]: non-exhaustive patterns: `i8::MAX` not covered
38-
--> $DIR/exhaustiveness.rs:51:8
38+
--> $DIR/exhaustiveness.rs:52:8
3939
|
4040
LL | m!(0i8, -128..127);
4141
| ^^^ pattern `i8::MAX` not covered
@@ -44,7 +44,7 @@ LL | m!(0i8, -128..127);
4444
= note: the matched value is of type `i8`
4545

4646
error[E0004]: non-exhaustive patterns: `i8::MAX` not covered
47-
--> $DIR/exhaustiveness.rs:52:8
47+
--> $DIR/exhaustiveness.rs:53:8
4848
|
4949
LL | m!(0i8, -128..=126);
5050
| ^^^ pattern `i8::MAX` not covered
@@ -53,7 +53,7 @@ LL | m!(0i8, -128..=126);
5353
= note: the matched value is of type `i8`
5454

5555
error[E0004]: non-exhaustive patterns: `i8::MIN` not covered
56-
--> $DIR/exhaustiveness.rs:53:8
56+
--> $DIR/exhaustiveness.rs:54:8
5757
|
5858
LL | m!(0i8, -127..=127);
5959
| ^^^ pattern `i8::MIN` not covered
@@ -62,7 +62,7 @@ LL | m!(0i8, -127..=127);
6262
= note: the matched value is of type `i8`
6363

6464
error[E0004]: non-exhaustive patterns: `0_i8` not covered
65-
--> $DIR/exhaustiveness.rs:54:11
65+
--> $DIR/exhaustiveness.rs:55:11
6666
|
6767
LL | match 0i8 {
6868
| ^^^ pattern `0_i8` not covered
@@ -71,7 +71,7 @@ LL | match 0i8 {
7171
= note: the matched value is of type `i8`
7272

7373
error[E0004]: non-exhaustive patterns: `u128::MAX` not covered
74-
--> $DIR/exhaustiveness.rs:59:8
74+
--> $DIR/exhaustiveness.rs:60:8
7575
|
7676
LL | m!(0u128, 0..=ALMOST_MAX);
7777
| ^^^^^ pattern `u128::MAX` not covered
@@ -80,7 +80,7 @@ LL | m!(0u128, 0..=ALMOST_MAX);
8080
= note: the matched value is of type `u128`
8181

8282
error[E0004]: non-exhaustive patterns: `5_u128..=u128::MAX` not covered
83-
--> $DIR/exhaustiveness.rs:60:8
83+
--> $DIR/exhaustiveness.rs:61:8
8484
|
8585
LL | m!(0u128, 0..=4);
8686
| ^^^^^ pattern `5_u128..=u128::MAX` not covered
@@ -89,7 +89,7 @@ LL | m!(0u128, 0..=4);
8989
= note: the matched value is of type `u128`
9090

9191
error[E0004]: non-exhaustive patterns: `0_u128` not covered
92-
--> $DIR/exhaustiveness.rs:61:8
92+
--> $DIR/exhaustiveness.rs:62:8
9393
|
9494
LL | m!(0u128, 1..=u128::MAX);
9595
| ^^^^^ pattern `0_u128` not covered
@@ -98,7 +98,7 @@ LL | m!(0u128, 1..=u128::MAX);
9898
= note: the matched value is of type `u128`
9999

100100
error[E0004]: non-exhaustive patterns: `(126_u8..=127_u8, false)` not covered
101-
--> $DIR/exhaustiveness.rs:69:11
101+
--> $DIR/exhaustiveness.rs:70:11
102102
|
103103
LL | match (0u8, true) {
104104
| ^^^^^^^^^^^ pattern `(126_u8..=127_u8, false)` not covered
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(exclusive_range_pattern)]
2-
#![deny(overlapping_patterns)]
2+
#![deny(overlapping_range_endpoints)]
33

44
macro_rules! m {
55
($s:expr, $t1:pat, $t2:pat) => {
@@ -12,27 +12,33 @@ macro_rules! m {
1212
}
1313

1414
fn main() {
15-
m!(0u8, 20..=30, 30..=40); //~ ERROR multiple patterns covering the same range
16-
m!(0u8, 30..=40, 20..=30); //~ ERROR multiple patterns covering the same range
15+
m!(0u8, 20..=30, 30..=40); //~ ERROR multiple patterns overlap on their endpoints
16+
m!(0u8, 30..=40, 20..=30); //~ ERROR multiple patterns overlap on their endpoints
1717
m!(0u8, 20..=30, 31..=40);
1818
m!(0u8, 20..=30, 29..=40);
19-
m!(0u8, 20.. 30, 29..=40); //~ ERROR multiple patterns covering the same range
19+
m!(0u8, 20.. 30, 29..=40); //~ ERROR multiple patterns overlap on their endpoints
2020
m!(0u8, 20.. 30, 28..=40);
2121
m!(0u8, 20.. 30, 30..=40);
2222
m!(0u8, 20..=30, 30..=30);
23-
m!(0u8, 20..=30, 30..=31); //~ ERROR multiple patterns covering the same range
23+
m!(0u8, 20..=30, 30..=31); //~ ERROR multiple patterns overlap on their endpoints
2424
m!(0u8, 20..=30, 29..=30);
2525
m!(0u8, 20..=30, 20..=20);
2626
m!(0u8, 20..=30, 20..=21);
27-
m!(0u8, 20..=30, 19..=20); //~ ERROR multiple patterns covering the same range
27+
m!(0u8, 20..=30, 19..=20); //~ ERROR multiple patterns overlap on their endpoints
2828
m!(0u8, 20..=30, 20);
2929
m!(0u8, 20..=30, 25);
3030
m!(0u8, 20..=30, 30);
3131
m!(0u8, 20.. 30, 29);
32-
m!(0u8, 20, 20..=30); //~ ERROR multiple patterns covering the same range
32+
m!(0u8, 20, 20..=30);
3333
m!(0u8, 25, 20..=30);
34-
m!(0u8, 30, 20..=30); //~ ERROR multiple patterns covering the same range
34+
m!(0u8, 30, 20..=30);
3535

36+
match 0u8 {
37+
0..=10 => {}
38+
20..=30 => {}
39+
10..=20 => {} //~ ERROR multiple patterns overlap on their endpoints
40+
_ => {}
41+
}
3642
match (0u8, true) {
3743
(0..=10, true) => {}
3844
(10..20, true) => {} // not detected
@@ -41,13 +47,13 @@ fn main() {
4147
}
4248
match (true, 0u8) {
4349
(true, 0..=10) => {}
44-
(true, 10..20) => {} //~ ERROR multiple patterns covering the same range
50+
(true, 10..20) => {} //~ ERROR multiple patterns overlap on their endpoints
4551
(false, 10..20) => {}
4652
_ => {}
4753
}
4854
match Some(0u8) {
4955
Some(0..=10) => {}
50-
Some(10..20) => {} //~ ERROR multiple patterns covering the same range
56+
Some(10..20) => {} //~ ERROR multiple patterns overlap on their endpoints
5157
_ => {}
5258
}
5359
}

0 commit comments

Comments
 (0)