Skip to content

Commit 36eced4

Browse files
committed
Cap the number of patterns pointed to by the lint
1 parent efb28bd commit 36eced4

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ mir_build_union_pattern = cannot use unions in constant patterns
327327
328328
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
329329
330+
mir_build_unreachable_making_this_unreachable_n_more = ...and {$covered_by_many_n_more_count} other patterns collectively make this unreachable
331+
330332
mir_build_unreachable_matches_same_values = matches some of the same values
331333
332334
mir_build_unreachable_pattern = unreachable pattern

compiler/rustc_mir_build/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ pub(crate) struct UnreachablePattern<'tcx> {
596596
pub(crate) covered_by_one: Option<Span>,
597597
#[note(mir_build_unreachable_covered_by_many)]
598598
pub(crate) covered_by_many: Option<MultiSpan>,
599+
pub(crate) covered_by_many_n_more_count: usize,
599600
}
600601

601602
#[derive(Subdiagnostic)]

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
917917
pat: &DeconstructedPat<'p, 'tcx>,
918918
explanation: &RedundancyExplanation<'p, 'tcx>,
919919
) {
920+
static CAP_COVERED_BY_MANY: usize = 4;
920921
let pat_span = pat.data().span;
921922
let mut lint = UnreachablePattern {
922923
span: Some(pat_span),
@@ -925,6 +926,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
925926
covered_by_catchall: None,
926927
covered_by_one: None,
927928
covered_by_many: None,
929+
covered_by_many_n_more_count: 0,
928930
};
929931
match explanation.covered_by.as_slice() {
930932
[] => {
@@ -950,15 +952,27 @@ fn report_unreachable_pattern<'p, 'tcx>(
950952
lint.covered_by_one = Some(covering_pat.data().span);
951953
}
952954
covering_pats => {
955+
let mut iter = covering_pats.iter();
953956
let mut multispan = MultiSpan::from_span(pat_span);
954-
for p in covering_pats {
957+
for p in iter.by_ref().take(CAP_COVERED_BY_MANY) {
955958
multispan.push_span_label(
956959
p.data().span,
957960
fluent::mir_build_unreachable_matches_same_values,
958961
);
959962
}
960-
multispan
961-
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable);
963+
let remain = iter.count();
964+
if remain == 0 {
965+
multispan.push_span_label(
966+
pat_span,
967+
fluent::mir_build_unreachable_making_this_unreachable,
968+
);
969+
} else {
970+
lint.covered_by_many_n_more_count = remain;
971+
multispan.push_span_label(
972+
pat_span,
973+
fluent::mir_build_unreachable_making_this_unreachable_n_more,
974+
);
975+
}
962976
lint.covered_by_many = Some(multispan);
963977
}
964978
}

tests/ui/pattern/usefulness/explain-unreachable-pats.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ fn main() {
2626
_ => {}
2727
}
2828

29+
match 0u8 {
30+
1 => {}
31+
//~^ NOTE matches some of the same values
32+
2 => {}
33+
//~^ NOTE matches some of the same values
34+
3 => {}
35+
//~^ NOTE matches some of the same values
36+
4 => {}
37+
//~^ NOTE matches some of the same values
38+
5 => {}
39+
6 => {}
40+
1 ..= 6 => {}
41+
//~^ ERROR unreachable pattern
42+
//~| NOTE no value can reach this
43+
//~| NOTE multiple earlier patterns match some of the same values
44+
//~| NOTE ...and 2 other patterns
45+
_ => {}
46+
}
47+
2948
let res: Result<(),!> = Ok(());
3049
match res {
3150
Ok(_) => {}

tests/ui/pattern/usefulness/explain-unreachable-pats.stderr

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,31 @@ LL | (1 | 2,) => {}
3232
| ^^^^^^^^ collectively making this unreachable
3333

3434
error: unreachable pattern
35-
--> $DIR/explain-unreachable-pats.rs:32:9
35+
--> $DIR/explain-unreachable-pats.rs:40:9
36+
|
37+
LL | 1 ..= 6 => {}
38+
| ^^^^^^^ no value can reach this
39+
|
40+
note: multiple earlier patterns match some of the same values
41+
--> $DIR/explain-unreachable-pats.rs:40:9
42+
|
43+
LL | 1 => {}
44+
| - matches some of the same values
45+
LL |
46+
LL | 2 => {}
47+
| - matches some of the same values
48+
LL |
49+
LL | 3 => {}
50+
| - matches some of the same values
51+
LL |
52+
LL | 4 => {}
53+
| - matches some of the same values
54+
...
55+
LL | 1 ..= 6 => {}
56+
| ^^^^^^^ ...and 2 other patterns collectively make this unreachable
57+
58+
error: unreachable pattern
59+
--> $DIR/explain-unreachable-pats.rs:51:9
3660
|
3761
LL | Err(_) => {}
3862
| ^^^^^^
@@ -41,7 +65,7 @@ LL | Err(_) => {}
4165
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
4266

4367
error: unreachable pattern
44-
--> $DIR/explain-unreachable-pats.rs:46:9
68+
--> $DIR/explain-unreachable-pats.rs:65:9
4569
|
4670
LL | (Err(_), Err(_)) => {}
4771
| ^^^^^^^^^^^^^^^^
@@ -50,7 +74,7 @@ LL | (Err(_), Err(_)) => {}
5074
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
5175

5276
error: unreachable pattern
53-
--> $DIR/explain-unreachable-pats.rs:53:9
77+
--> $DIR/explain-unreachable-pats.rs:72:9
5478
|
5579
LL | (Err(_), Err(_)) => {}
5680
| ^^^^^^^^^^^^^^^^
@@ -59,7 +83,7 @@ LL | (Err(_), Err(_)) => {}
5983
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
6084

6185
error: unreachable pattern
62-
--> $DIR/explain-unreachable-pats.rs:63:11
86+
--> $DIR/explain-unreachable-pats.rs:82:11
6387
|
6488
LL | if let (0
6589
| - matches all the relevant values
@@ -68,13 +92,13 @@ LL | | 0, _) = (0, 0) {}
6892
| ^ no value can reach this
6993

7094
error: unreachable pattern
71-
--> $DIR/explain-unreachable-pats.rs:73:9
95+
--> $DIR/explain-unreachable-pats.rs:92:9
7296
|
7397
LL | (_, true) => {}
7498
| ^^^^^^^^^ no value can reach this
7599
|
76100
note: multiple earlier patterns match some of the same values
77-
--> $DIR/explain-unreachable-pats.rs:73:9
101+
--> $DIR/explain-unreachable-pats.rs:92:9
78102
|
79103
LL | (true, _) => {}
80104
| --------- matches some of the same values
@@ -86,7 +110,7 @@ LL | (_, true) => {}
86110
| ^^^^^^^^^ collectively making this unreachable
87111

88112
error: unreachable pattern
89-
--> $DIR/explain-unreachable-pats.rs:86:9
113+
--> $DIR/explain-unreachable-pats.rs:105:9
90114
|
91115
LL | (true, _) => {}
92116
| --------- matches all the relevant values
@@ -95,13 +119,13 @@ LL | (true, true) => {}
95119
| ^^^^^^^^^^^^ no value can reach this
96120

97121
error: unreachable pattern
98-
--> $DIR/explain-unreachable-pats.rs:98:9
122+
--> $DIR/explain-unreachable-pats.rs:117:9
99123
|
100124
LL | (_, true, 0..10) => {}
101125
| ---------------- matches all the relevant values
102126
...
103127
LL | (_, true, 3) => {}
104128
| ^^^^^^^^^^^^ no value can reach this
105129

106-
error: aborting due to 9 previous errors
130+
error: aborting due to 10 previous errors
107131

0 commit comments

Comments
 (0)