Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 06e2344

Browse files
committed
Almost check overlapping_range lint level per arm
1 parent 554fa01 commit 06e2344

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
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
@@ -303,9 +303,8 @@ impl IntRange {
303303
pub(super) fn lint_overlapping_range_endpoints<'a, 'p: 'a, 'tcx: 'a>(
304304
&self,
305305
pcx: &PatCtxt<'_, 'p, 'tcx>,
306-
pats: impl Iterator<Item = (&'a DeconstructedPat<'p, 'tcx>, bool)>,
306+
pats: impl Iterator<Item = (&'a DeconstructedPat<'p, 'tcx>, bool, HirId)>,
307307
column_count: usize,
308-
lint_root: HirId,
309308
) {
310309
// FIXME: for now, only check for overlapping ranges on non-nested range patterns. Otherwise
311310
// with the current logic the following is detected as overlapping:
@@ -325,9 +324,11 @@ impl IntRange {
325324
let mut prefixes: SmallVec<[_; 1]> = Default::default();
326325
let mut suffixes: SmallVec<[_; 1]> = Default::default();
327326
// Iterate on rows that contained `overlap`.
328-
for (range, this_span, is_under_guard) in pats.filter_map(|(pat, under_guard)| {
329-
Some((pat.ctor().as_int_range()?, pat.span(), under_guard))
330-
}) {
327+
for (range, this_span, is_under_guard, arm_hir_id) in
328+
pats.filter_map(|(pat, under_guard, arm_hir_id)| {
329+
Some((pat.ctor().as_int_range()?, pat.span(), under_guard, arm_hir_id))
330+
})
331+
{
331332
if range.is_singleton() {
332333
continue;
333334
}
@@ -355,7 +356,7 @@ impl IntRange {
355356
.collect();
356357
pcx.cx.tcx.emit_spanned_lint(
357358
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
358-
lint_root,
359+
arm_hir_id,
359360
this_span,
360361
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
361362
);

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,6 @@ fn compute_usefulness<'p, 'tcx>(
811811
// For each constructor, we compute whether there's a value that starts with it that would
812812
// witness the usefulness of `v`.
813813
let mut ret = WitnessMatrix::new_empty();
814-
let orig_column_count = matrix.column_count();
815814
for ctor in split_ctors {
816815
debug!("specialize({:?})", ctor);
817816
let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor);
@@ -826,15 +825,20 @@ fn compute_usefulness<'p, 'tcx>(
826825
// If two ranges overlap on their boundaries, that boundary will be found as a singleton
827826
// range after splitting.
828827
// We limit to a single column for now, see `lint_overlapping_range_endpoints`.
829-
if overlap_range.is_singleton() && orig_column_count == 1 {
828+
if overlap_range.is_singleton() && matrix.column_count() == 1 {
830829
overlap_range.lint_overlapping_range_endpoints(
831830
pcx,
832-
spec_matrix
833-
.rows()
834-
.map(|child_row| &matrix.rows[child_row.parent_row])
835-
.map(|parent_row| (parent_row.head(), parent_row.is_under_guard)),
836-
orig_column_count,
837-
lint_root,
831+
spec_matrix.rows().map(|child_row| &matrix.rows[child_row.parent_row]).map(
832+
|parent_row| {
833+
(
834+
parent_row.head(),
835+
parent_row.is_under_guard,
836+
// parent_row.arm_hir_id,
837+
lint_root,
838+
)
839+
},
840+
),
841+
matrix.column_count(),
838842
);
839843
}
840844
}

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

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

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,29 @@ 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:52:9
92+
|
93+
LL | 0..=10 => {}
94+
| ------ this range overlaps on `10_u8`...
95+
...
96+
LL | 10..=20 => {}
97+
| ^^^^^^^ ... with this range
98+
|
99+
= note: you likely meant to write mutually exclusive ranges
100+
101+
error: multiple patterns overlap on their endpoints
102+
--> $DIR/overlapping_range_endpoints.rs:58:9
81103
|
82104
LL | 0..=10 => {}
83105
| ------ this range overlaps on `10_u8`...
@@ -87,7 +109,7 @@ LL | 10..=20 if true => {}
87109
= note: you likely meant to write mutually exclusive ranges
88110

89111
error: multiple patterns overlap on their endpoints
90-
--> $DIR/overlapping_range_endpoints.rs:63:16
112+
--> $DIR/overlapping_range_endpoints.rs:74:16
91113
|
92114
LL | (true, 0..=10) => {}
93115
| ------ this range overlaps on `10_u8`...
@@ -97,7 +119,7 @@ LL | (true, 10..20) => {}
97119
= note: you likely meant to write mutually exclusive ranges
98120

99121
error: multiple patterns overlap on their endpoints
100-
--> $DIR/overlapping_range_endpoints.rs:71:13
122+
--> $DIR/overlapping_range_endpoints.rs:82:13
101123
|
102124
LL | (true, 0..=10) => {}
103125
| ------ this range overlaps on `10_u8`...
@@ -108,7 +130,7 @@ LL | (_, 10..20) => {}
108130
= note: you likely meant to write mutually exclusive ranges
109131

110132
error: multiple patterns overlap on their endpoints
111-
--> $DIR/overlapping_range_endpoints.rs:76:14
133+
--> $DIR/overlapping_range_endpoints.rs:87:14
112134
|
113135
LL | Some(0..=10) => {}
114136
| ------ this range overlaps on `10_u8`...
@@ -117,5 +139,5 @@ LL | Some(10..20) => {}
117139
|
118140
= note: you likely meant to write mutually exclusive ranges
119141

120-
error: aborting due to 11 previous errors
142+
error: aborting due to 13 previous errors
121143

0 commit comments

Comments
 (0)