Skip to content

Commit

Permalink
Only suggest the fix for empty patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Aug 19, 2024
1 parent 8190fd1 commit a0ffb44
Show file tree
Hide file tree
Showing 37 changed files with 137 additions and 651 deletions.
9 changes: 5 additions & 4 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
hir_id: HirId,
pat: &DeconstructedPat<'p, 'tcx>,
explanation: &RedundancyExplanation<'p, 'tcx>,
suggest_remove: Option<Span>,
whole_arm_span: Option<Span>,
) {
let pat_span = pat.data().span;
let mut lint = UnreachablePattern {
Expand All @@ -930,12 +930,13 @@ fn report_unreachable_pattern<'p, 'tcx>(
covered_by_catchall: None,
covered_by_one: None,
covered_by_many: None,
suggest_remove,
suggest_remove: None,
};
match explanation.covered_by.as_slice() {
[] => {
// Empty pattern; we report the uninhabited type that caused the emptiness.
lint.span = None; // Don't label the pattern itself
lint.suggest_remove = whole_arm_span; // Suggest to remove the match arm
pat.walk(&mut |subpat| {
let ty = **subpat.ty();
if cx.is_uninhabited(ty) {
Expand Down Expand Up @@ -981,7 +982,7 @@ fn report_arm_reachability<'p, 'tcx>(
if let Usefulness::Redundant(explanation) = is_useful {
let hir_id = arm.arm_data;
let arm_span = cx.tcx.hir().span(hir_id);
let suggest_remove = if is_match_arm {
let whole_arm_span = if is_match_arm {
// If the arm is followed by a comma, extend the span to include it.
let with_whitespace = sm.span_extend_while_whitespace(arm_span);
if let Some(comma) = sm.span_look_ahead(with_whitespace, ",", Some(1)) {
Expand All @@ -992,7 +993,7 @@ fn report_arm_reachability<'p, 'tcx>(
} else {
None
};
report_unreachable_pattern(cx, hir_id, arm.pat, explanation, suggest_remove)
report_unreachable_pattern(cx, hir_id, arm.pat, explanation, whole_arm_span)
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/consts/packed_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ warning: unreachable pattern
LL | Foo { field: (5, 6, 7, 8) } => {},
| --------------------------- matches all the values already
LL | FOO => unreachable!(),
| ^^^-------------------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern
|
= note: `#[warn(unreachable_patterns)]` on by default

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/consts/packed_pattern2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ warning: unreachable pattern
LL | Bar { a: Foo { field: (5, 6) } } => {},
| -------------------------------- matches all the values already
LL | FOO => unreachable!(),
| ^^^-------------------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern
|
= note: `#[warn(unreachable_patterns)]` on by default

Expand Down
5 changes: 1 addition & 4 deletions tests/ui/error-codes/E0001.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error: unreachable pattern
--> $DIR/E0001.rs:8:9
|
LL | _ => {/* ... */}
| ^---------------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/E0001.rs:8:9
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/lint/issue-30302.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ LL | Nil => true,
| --- matches any value
LL |
LL | _ => false
| ^---------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern
|
note: the lint level is defined here
--> $DIR/issue-30302.rs:4:9
Expand Down
55 changes: 11 additions & 44 deletions tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ error: unreachable pattern
LL | (1 | 2,) => {}
| -------- matches all the values already
LL | (1,) => {}
| ^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^ unreachable pattern
|
note: the lint level is defined here
--> $DIR/exhaustiveness-unreachable-pattern.rs:1:9
Expand All @@ -21,19 +18,13 @@ error: unreachable pattern
LL | (1 | 2,) => {}
| -------- matches all the values already
LL | (2,) => {}
| ^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
|
LL | (1 | 2,) => {}
| ^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
Expand All @@ -51,10 +42,7 @@ error: unreachable pattern
LL | (1 | 2, 3 | 4) => {}
| -------------- matches all the values already
LL | (1, 3) => {}
| ^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
Expand All @@ -63,10 +51,7 @@ LL | (1 | 2, 3 | 4) => {}
| -------------- matches all the values already
LL | (1, 3) => {}
LL | (1, 4) => {}
| ^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
Expand All @@ -75,10 +60,7 @@ LL | (1 | 2, 3 | 4) => {}
| -------------- matches all the values already
...
LL | (2, 4) => {}
| ^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:27:9
Expand All @@ -87,19 +69,13 @@ LL | (1 | 2, 3 | 4) => {}
| -------------- matches all the values already
...
LL | (2 | 1, 4) => {}
| ^^^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
|
LL | (1, 4 | 5) => {}
| ^^^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^^^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
Expand All @@ -126,10 +102,7 @@ error: unreachable pattern
LL | (None | Some(1 | 2),) => {}
| --------------------- matches all the values already
LL | (Some(1),) => {}
| ^^^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:43:9
Expand All @@ -138,21 +111,15 @@ LL | (None | Some(1 | 2),) => {}
| --------------------- matches all the values already
LL | (Some(1),) => {}
LL | (None,) => {}
| ^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:48:9
|
LL | ((1 | 2,) | (3 | 4,),) => {}
| ---------------------- matches all the values already
LL | ((1..=4,),) => {}
| ^^^^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/exhaustiveness-unreachable-pattern.rs:53:14
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/pattern/issue-14221.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ LL | A => "A",
| - matches any value
LL |
LL | B => "B",
| ^--------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern
|
note: the lint level is defined here
--> $DIR/issue-14221.rs:1:9
Expand Down
45 changes: 9 additions & 36 deletions tests/ui/pattern/usefulness/consts-opaque.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ error: unreachable pattern
LL | Bar => {}
| --- matches any value
LL | BAR => {}
| ^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern
|
note: the lint level is defined here
--> $DIR/consts-opaque.rs:6:9
Expand All @@ -70,21 +67,15 @@ LL | Bar => {}
| --- matches any value
...
LL | _ => {}
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:56:9
|
LL | BAR => {}
| --- matches any value
LL | Bar => {}
| ^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:58:9
Expand All @@ -93,21 +84,15 @@ LL | BAR => {}
| --- matches any value
...
LL | _ => {}
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:64:9
|
LL | BAR => {}
| --- matches any value
LL | BAR => {} // should not be emitting unreachable warning
| ^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:66:9
Expand All @@ -116,41 +101,29 @@ LL | BAR => {}
| --- matches any value
...
LL | _ => {} // should not be emitting unreachable warning
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:72:9
|
LL | BAZ => {}
| --- matches all the values already
LL | Baz::Baz1 => {} // should not be emitting unreachable warning
| ^^^^^^^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^^^^^^^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:79:9
|
LL | Baz::Baz1 => {}
| --------- matches all the values already
LL | BAZ => {}
| ^^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^^ unreachable pattern

error: unreachable pattern
--> $DIR/consts-opaque.rs:87:9
|
LL | _ => {} // should not be emitting unreachable warning
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern
|
note: these patterns collectively make the last one unreachable
--> $DIR/consts-opaque.rs:87:9
Expand Down
20 changes: 4 additions & 16 deletions tests/ui/pattern/usefulness/empty-types.exhaustive_patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ error: unreachable pattern
LL | None => {}
| ---- matches all the values already
LL | _ => {}
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error: unreachable pattern
--> $DIR/empty-types.rs:199:13
Expand Down Expand Up @@ -445,10 +442,7 @@ error: unreachable pattern
LL | [] => {}
| -- matches all the values already
LL | _ => {}
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error[E0004]: non-exhaustive patterns: `[]` not covered
--> $DIR/empty-types.rs:397:11
Expand Down Expand Up @@ -491,10 +485,7 @@ LL | None => {}
| ---- matches all the values already
LL | // !useful, !reachable
LL | _ => {}
| ^------
| |
| unreachable pattern
| help: remove the match arm
| ^ unreachable pattern

error: unreachable pattern
--> $DIR/empty-types.rs:431:9
Expand All @@ -503,10 +494,7 @@ LL | None => {}
| ---- matches all the values already
LL | // !useful, !reachable
LL | _a => {}
| ^^------
| |
| unreachable pattern
| help: remove the match arm
| ^^ unreachable pattern

error: unreachable pattern
--> $DIR/empty-types.rs:603:9
Expand Down
Loading

0 comments on commit a0ffb44

Please sign in to comment.