Skip to content

Commit 76c6d5f

Browse files
committed
Check overlapping_range lint level per arm
1 parent dc732b8 commit 76c6d5f

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,8 @@ impl IntRange {
215215
pub(super) fn lint_overlapping_range_endpoints<'a, 'p: 'a, 'tcx: 'a>(
216216
&self,
217217
pcx: &PatCtxt<'_, 'p, 'tcx>,
218-
pats: impl Iterator<Item = (&'a DeconstructedPat<'p, 'tcx>, bool)>,
218+
pats: impl Iterator<Item = (&'a DeconstructedPat<'p, 'tcx>, bool, HirId)>,
219219
column_count: usize,
220-
lint_root: HirId,
221220
) {
222221
// FIXME: for now, only check for overlapping ranges on non-nested range patterns. Otherwise
223222
// with the current logic the following is detected as overlapping:
@@ -237,9 +236,11 @@ impl IntRange {
237236
let mut prefixes: SmallVec<[_; 1]> = Default::default();
238237
let mut suffixes: SmallVec<[_; 1]> = Default::default();
239238
// Iterate on rows that contained `overlap`.
240-
for (range, this_span, is_under_guard) in pats.filter_map(|(pat, under_guard)| {
241-
Some((pat.ctor().as_int_range()?, pat.span(), under_guard))
242-
}) {
239+
for (range, this_span, is_under_guard, arm_hir_id) in
240+
pats.filter_map(|(pat, under_guard, arm_hir_id)| {
241+
Some((pat.ctor().as_int_range()?, pat.span(), under_guard, arm_hir_id))
242+
})
243+
{
243244
if range.is_singleton() {
244245
continue;
245246
}
@@ -268,7 +269,7 @@ impl IntRange {
268269
.collect();
269270
pcx.cx.tcx.emit_spanned_lint(
270271
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
271-
lint_root,
272+
arm_hir_id,
272273
this_span,
273274
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
274275
);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,16 @@ fn compute_usefulness<'p, 'tcx>(
806806
if overlap_range.is_singleton() && orig_column_count == 1 {
807807
overlap_range.lint_overlapping_range_endpoints(
808808
pcx,
809-
spec_matrix
810-
.rows()
811-
.map(|child_row| &matrix.rows[child_row.parent_row])
812-
.map(|parent_row| (parent_row.head(), parent_row.is_under_guard)),
809+
spec_matrix.rows().map(|child_row| &matrix.rows[child_row.parent_row]).map(
810+
|parent_row| {
811+
(
812+
parent_row.head(),
813+
parent_row.is_under_guard,
814+
parent_row.arm_hir_id,
815+
)
816+
},
817+
),
813818
orig_column_count,
814-
lint_root,
815819
);
816820
}
817821
}

tests/ui/pattern/usefulness/integer-ranges/overlapping_range_endpoints.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ fn main() {
4242
//~| ERROR multiple patterns overlap on their endpoints
4343
_ => {}
4444
}
45+
// Selectively allow lint
46+
match 0u8 {
47+
0..=10 => {}
48+
#[allow(overlapping_range_endpoints)]
49+
10..=20 => {}
50+
10..=20 => {}
51+
//~^ ERROR multiple patterns overlap on their endpoints
52+
_ => {}
53+
}
4554
match 0u8 {
4655
0..=10 => {}
4756
10..=20 if true => {} //~ ERROR multiple patterns overlap on their endpoints

tests/ui/pattern/usefulness/integer-ranges/overlapping_range_endpoints.stderr

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,18 @@ LL | 10..=20 => {}
7777
= note: you likely meant to write mutually exclusive ranges
7878

7979
error: multiple patterns overlap on their endpoints
80-
--> $DIR/overlapping_range_endpoints.rs:47:9
80+
--> $DIR/overlapping_range_endpoints.rs:50:9
81+
|
82+
LL | 0..=10 => {}
83+
| ------ this range overlaps on `10_u8`...
84+
...
85+
LL | 10..=20 => {}
86+
| ^^^^^^^ ... with this range
87+
|
88+
= note: you likely meant to write mutually exclusive ranges
89+
90+
error: multiple patterns overlap on their endpoints
91+
--> $DIR/overlapping_range_endpoints.rs:56:9
8192
|
8293
LL | 0..=10 => {}
8394
| ------ this range overlaps on `10_u8`...
@@ -87,7 +98,7 @@ LL | 10..=20 if true => {}
8798
= note: you likely meant to write mutually exclusive ranges
8899

89100
error: multiple patterns overlap on their endpoints
90-
--> $DIR/overlapping_range_endpoints.rs:63:16
101+
--> $DIR/overlapping_range_endpoints.rs:72:16
91102
|
92103
LL | (true, 0..=10) => {}
93104
| ------ this range overlaps on `10_u8`...
@@ -97,7 +108,7 @@ LL | (true, 10..20) => {}
97108
= note: you likely meant to write mutually exclusive ranges
98109

99110
error: multiple patterns overlap on their endpoints
100-
--> $DIR/overlapping_range_endpoints.rs:71:13
111+
--> $DIR/overlapping_range_endpoints.rs:80:13
101112
|
102113
LL | (true, 0..=10) => {}
103114
| ------ this range overlaps on `10_u8`...
@@ -108,7 +119,7 @@ LL | (_, 10..20) => {}
108119
= note: you likely meant to write mutually exclusive ranges
109120

110121
error: multiple patterns overlap on their endpoints
111-
--> $DIR/overlapping_range_endpoints.rs:76:14
122+
--> $DIR/overlapping_range_endpoints.rs:85:14
112123
|
113124
LL | Some(0..=10) => {}
114125
| ------ this range overlaps on `10_u8`...
@@ -117,5 +128,5 @@ LL | Some(10..20) => {}
117128
|
118129
= note: you likely meant to write mutually exclusive ranges
119130

120-
error: aborting due to 11 previous errors
131+
error: aborting due to 12 previous errors
121132

0 commit comments

Comments
 (0)