1
Fork 0

Cap the number of patterns pointed to by the lint

This commit is contained in:
Nadrieril 2024-08-19 21:26:32 +02:00
parent efb28bdd90
commit 36eced444e
5 changed files with 72 additions and 12 deletions

View file

@ -327,6 +327,8 @@ mir_build_union_pattern = cannot use unions in constant patterns
mir_build_unreachable_making_this_unreachable = collectively making this unreachable mir_build_unreachable_making_this_unreachable = collectively making this unreachable
mir_build_unreachable_making_this_unreachable_n_more = ...and {$covered_by_many_n_more_count} other patterns collectively make this unreachable
mir_build_unreachable_matches_same_values = matches some of the same values mir_build_unreachable_matches_same_values = matches some of the same values
mir_build_unreachable_pattern = unreachable pattern mir_build_unreachable_pattern = unreachable pattern

View file

@ -596,6 +596,7 @@ pub(crate) struct UnreachablePattern<'tcx> {
pub(crate) covered_by_one: Option<Span>, pub(crate) covered_by_one: Option<Span>,
#[note(mir_build_unreachable_covered_by_many)] #[note(mir_build_unreachable_covered_by_many)]
pub(crate) covered_by_many: Option<MultiSpan>, pub(crate) covered_by_many: Option<MultiSpan>,
pub(crate) covered_by_many_n_more_count: usize,
} }
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]

View file

@ -917,6 +917,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
pat: &DeconstructedPat<'p, 'tcx>, pat: &DeconstructedPat<'p, 'tcx>,
explanation: &RedundancyExplanation<'p, 'tcx>, explanation: &RedundancyExplanation<'p, 'tcx>,
) { ) {
static CAP_COVERED_BY_MANY: usize = 4;
let pat_span = pat.data().span; let pat_span = pat.data().span;
let mut lint = UnreachablePattern { let mut lint = UnreachablePattern {
span: Some(pat_span), span: Some(pat_span),
@ -925,6 +926,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
covered_by_catchall: None, covered_by_catchall: None,
covered_by_one: None, covered_by_one: None,
covered_by_many: None, covered_by_many: None,
covered_by_many_n_more_count: 0,
}; };
match explanation.covered_by.as_slice() { match explanation.covered_by.as_slice() {
[] => { [] => {
@ -950,15 +952,27 @@ fn report_unreachable_pattern<'p, 'tcx>(
lint.covered_by_one = Some(covering_pat.data().span); lint.covered_by_one = Some(covering_pat.data().span);
} }
covering_pats => { covering_pats => {
let mut iter = covering_pats.iter();
let mut multispan = MultiSpan::from_span(pat_span); let mut multispan = MultiSpan::from_span(pat_span);
for p in covering_pats { for p in iter.by_ref().take(CAP_COVERED_BY_MANY) {
multispan.push_span_label( multispan.push_span_label(
p.data().span, p.data().span,
fluent::mir_build_unreachable_matches_same_values, fluent::mir_build_unreachable_matches_same_values,
); );
} }
multispan let remain = iter.count();
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable); if remain == 0 {
multispan.push_span_label(
pat_span,
fluent::mir_build_unreachable_making_this_unreachable,
);
} else {
lint.covered_by_many_n_more_count = remain;
multispan.push_span_label(
pat_span,
fluent::mir_build_unreachable_making_this_unreachable_n_more,
);
}
lint.covered_by_many = Some(multispan); lint.covered_by_many = Some(multispan);
} }
} }

View file

@ -26,6 +26,25 @@ fn main() {
_ => {} _ => {}
} }
match 0u8 {
1 => {}
//~^ NOTE matches some of the same values
2 => {}
//~^ NOTE matches some of the same values
3 => {}
//~^ NOTE matches some of the same values
4 => {}
//~^ NOTE matches some of the same values
5 => {}
6 => {}
1 ..= 6 => {}
//~^ ERROR unreachable pattern
//~| NOTE no value can reach this
//~| NOTE multiple earlier patterns match some of the same values
//~| NOTE ...and 2 other patterns
_ => {}
}
let res: Result<(),!> = Ok(()); let res: Result<(),!> = Ok(());
match res { match res {
Ok(_) => {} Ok(_) => {}

View file

@ -32,7 +32,31 @@ LL | (1 | 2,) => {}
| ^^^^^^^^ collectively making this unreachable | ^^^^^^^^ collectively making this unreachable
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:32:9 --> $DIR/explain-unreachable-pats.rs:40:9
|
LL | 1 ..= 6 => {}
| ^^^^^^^ no value can reach this
|
note: multiple earlier patterns match some of the same values
--> $DIR/explain-unreachable-pats.rs:40:9
|
LL | 1 => {}
| - matches some of the same values
LL |
LL | 2 => {}
| - matches some of the same values
LL |
LL | 3 => {}
| - matches some of the same values
LL |
LL | 4 => {}
| - matches some of the same values
...
LL | 1 ..= 6 => {}
| ^^^^^^^ ...and 2 other patterns collectively make this unreachable
error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:51:9
| |
LL | Err(_) => {} LL | Err(_) => {}
| ^^^^^^ | ^^^^^^
@ -41,7 +65,7 @@ LL | Err(_) => {}
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types = note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:46:9 --> $DIR/explain-unreachable-pats.rs:65:9
| |
LL | (Err(_), Err(_)) => {} LL | (Err(_), Err(_)) => {}
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
@ -50,7 +74,7 @@ LL | (Err(_), Err(_)) => {}
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types = note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:53:9 --> $DIR/explain-unreachable-pats.rs:72:9
| |
LL | (Err(_), Err(_)) => {} LL | (Err(_), Err(_)) => {}
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
@ -59,7 +83,7 @@ LL | (Err(_), Err(_)) => {}
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types = note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:63:11 --> $DIR/explain-unreachable-pats.rs:82:11
| |
LL | if let (0 LL | if let (0
| - matches all the relevant values | - matches all the relevant values
@ -68,13 +92,13 @@ LL | | 0, _) = (0, 0) {}
| ^ no value can reach this | ^ no value can reach this
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:73:9 --> $DIR/explain-unreachable-pats.rs:92:9
| |
LL | (_, true) => {} LL | (_, true) => {}
| ^^^^^^^^^ no value can reach this | ^^^^^^^^^ no value can reach this
| |
note: multiple earlier patterns match some of the same values note: multiple earlier patterns match some of the same values
--> $DIR/explain-unreachable-pats.rs:73:9 --> $DIR/explain-unreachable-pats.rs:92:9
| |
LL | (true, _) => {} LL | (true, _) => {}
| --------- matches some of the same values | --------- matches some of the same values
@ -86,7 +110,7 @@ LL | (_, true) => {}
| ^^^^^^^^^ collectively making this unreachable | ^^^^^^^^^ collectively making this unreachable
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:86:9 --> $DIR/explain-unreachable-pats.rs:105:9
| |
LL | (true, _) => {} LL | (true, _) => {}
| --------- matches all the relevant values | --------- matches all the relevant values
@ -95,7 +119,7 @@ LL | (true, true) => {}
| ^^^^^^^^^^^^ no value can reach this | ^^^^^^^^^^^^ no value can reach this
error: unreachable pattern error: unreachable pattern
--> $DIR/explain-unreachable-pats.rs:98:9 --> $DIR/explain-unreachable-pats.rs:117:9
| |
LL | (_, true, 0..10) => {} LL | (_, true, 0..10) => {}
| ---------------- matches all the relevant values | ---------------- matches all the relevant values
@ -103,5 +127,5 @@ LL | (_, true, 0..10) => {}
LL | (_, true, 3) => {} LL | (_, true, 3) => {}
| ^^^^^^^^^^^^ no value can reach this | ^^^^^^^^^^^^ no value can reach this
error: aborting due to 9 previous errors error: aborting due to 10 previous errors