Skip to content

Commit b006036

Browse files
committed
Only suggest the fix for empty patterns
1 parent 10ae774 commit b006036

37 files changed

+137
-651
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
921921
hir_id: HirId,
922922
pat: &DeconstructedPat<'p, 'tcx>,
923923
explanation: &RedundancyExplanation<'p, 'tcx>,
924-
suggest_remove: Option<Span>,
924+
whole_arm_span: Option<Span>,
925925
) {
926926
let pat_span = pat.data().span;
927927
let mut lint = UnreachablePattern {
@@ -930,12 +930,13 @@ fn report_unreachable_pattern<'p, 'tcx>(
930930
covered_by_catchall: None,
931931
covered_by_one: None,
932932
covered_by_many: None,
933-
suggest_remove,
933+
suggest_remove: None,
934934
};
935935
match explanation.covered_by.as_slice() {
936936
[] => {
937937
// Empty pattern; we report the uninhabited type that caused the emptiness.
938938
lint.span = None; // Don't label the pattern itself
939+
lint.suggest_remove = whole_arm_span; // Suggest to remove the match arm
939940
pat.walk(&mut |subpat| {
940941
let ty = **subpat.ty();
941942
if cx.is_uninhabited(ty) {
@@ -981,7 +982,7 @@ fn report_arm_reachability<'p, 'tcx>(
981982
if let Usefulness::Redundant(explanation) = is_useful {
982983
let hir_id = arm.arm_data;
983984
let arm_span = cx.tcx.hir().span(hir_id);
984-
let suggest_remove = if is_match_arm {
985+
let whole_arm_span = if is_match_arm {
985986
// If the arm is followed by a comma, extend the span to include it.
986987
let with_whitespace = sm.span_extend_while_whitespace(arm_span);
987988
if let Some(comma) = sm.span_look_ahead(with_whitespace, ",", Some(1)) {
@@ -992,7 +993,7 @@ fn report_arm_reachability<'p, 'tcx>(
992993
} else {
993994
None
994995
};
995-
report_unreachable_pattern(cx, hir_id, arm.pat, explanation, suggest_remove)
996+
report_unreachable_pattern(cx, hir_id, arm.pat, explanation, whole_arm_span)
996997
}
997998
}
998999
}

tests/ui/consts/packed_pattern.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ warning: unreachable pattern
44
LL | Foo { field: (5, 6, 7, 8) } => {},
55
| --------------------------- matches all the values already
66
LL | FOO => unreachable!(),
7-
| ^^^-------------------
8-
| |
9-
| unreachable pattern
10-
| help: remove the match arm
7+
| ^^^ unreachable pattern
118
|
129
= note: `#[warn(unreachable_patterns)]` on by default
1310

tests/ui/consts/packed_pattern2.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ warning: unreachable pattern
44
LL | Bar { a: Foo { field: (5, 6) } } => {},
55
| -------------------------------- matches all the values already
66
LL | FOO => unreachable!(),
7-
| ^^^-------------------
8-
| |
9-
| unreachable pattern
10-
| help: remove the match arm
7+
| ^^^ unreachable pattern
118
|
129
= note: `#[warn(unreachable_patterns)]` on by default
1310

tests/ui/error-codes/E0001.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error: unreachable pattern
22
--> $DIR/E0001.rs:8:9
33
|
44
LL | _ => {/* ... */}
5-
| ^---------------
6-
| |
7-
| unreachable pattern
8-
| help: remove the match arm
5+
| ^ unreachable pattern
96
|
107
note: these patterns collectively make the last one unreachable
118
--> $DIR/E0001.rs:8:9

tests/ui/lint/issue-30302.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ LL | Nil => true,
1313
| --- matches any value
1414
LL |
1515
LL | _ => false
16-
| ^---------
17-
| |
18-
| unreachable pattern
19-
| help: remove the match arm
16+
| ^ unreachable pattern
2017
|
2118
note: the lint level is defined here
2219
--> $DIR/issue-30302.rs:4:9

tests/ui/or-patterns/exhaustiveness-unreachable-pattern.stderr

+11-44
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ error: unreachable pattern
44
LL | (1 | 2,) => {}
55
| -------- matches all the values already
66
LL | (1,) => {}
7-
| ^^^^------
8-
| |
9-
| unreachable pattern
10-
| help: remove the match arm
7+
| ^^^^ unreachable pattern
118
|
129
note: the lint level is defined here
1310
--> $DIR/exhaustiveness-unreachable-pattern.rs:1:9
@@ -21,19 +18,13 @@ error: unreachable pattern
2118
LL | (1 | 2,) => {}
2219
| -------- matches all the values already
2320
LL | (2,) => {}
24-
| ^^^^------
25-
| |
26-
| unreachable pattern
27-
| help: remove the match arm
21+
| ^^^^ unreachable pattern
2822

2923
error: unreachable pattern
3024
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
3125
|
3226
LL | (1 | 2,) => {}
33-
| ^^^^^^^^------
34-
| |
35-
| unreachable pattern
36-
| help: remove the match arm
27+
| ^^^^^^^^ unreachable pattern
3728
|
3829
note: these patterns collectively make the last one unreachable
3930
--> $DIR/exhaustiveness-unreachable-pattern.rs:19:9
@@ -51,10 +42,7 @@ error: unreachable pattern
5142
LL | (1 | 2, 3 | 4) => {}
5243
| -------------- matches all the values already
5344
LL | (1, 3) => {}
54-
| ^^^^^^------
55-
| |
56-
| unreachable pattern
57-
| help: remove the match arm
45+
| ^^^^^^ unreachable pattern
5846

5947
error: unreachable pattern
6048
--> $DIR/exhaustiveness-unreachable-pattern.rs:25:9
@@ -63,10 +51,7 @@ LL | (1 | 2, 3 | 4) => {}
6351
| -------------- matches all the values already
6452
LL | (1, 3) => {}
6553
LL | (1, 4) => {}
66-
| ^^^^^^------
67-
| |
68-
| unreachable pattern
69-
| help: remove the match arm
54+
| ^^^^^^ unreachable pattern
7055

7156
error: unreachable pattern
7257
--> $DIR/exhaustiveness-unreachable-pattern.rs:26:9
@@ -75,10 +60,7 @@ LL | (1 | 2, 3 | 4) => {}
7560
| -------------- matches all the values already
7661
...
7762
LL | (2, 4) => {}
78-
| ^^^^^^------
79-
| |
80-
| unreachable pattern
81-
| help: remove the match arm
63+
| ^^^^^^ unreachable pattern
8264

8365
error: unreachable pattern
8466
--> $DIR/exhaustiveness-unreachable-pattern.rs:27:9
@@ -87,19 +69,13 @@ LL | (1 | 2, 3 | 4) => {}
8769
| -------------- matches all the values already
8870
...
8971
LL | (2 | 1, 4) => {}
90-
| ^^^^^^^^^^------
91-
| |
92-
| unreachable pattern
93-
| help: remove the match arm
72+
| ^^^^^^^^^^ unreachable pattern
9473

9574
error: unreachable pattern
9675
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
9776
|
9877
LL | (1, 4 | 5) => {}
99-
| ^^^^^^^^^^------
100-
| |
101-
| unreachable pattern
102-
| help: remove the match arm
78+
| ^^^^^^^^^^ unreachable pattern
10379
|
10480
note: these patterns collectively make the last one unreachable
10581
--> $DIR/exhaustiveness-unreachable-pattern.rs:29:9
@@ -126,10 +102,7 @@ error: unreachable pattern
126102
LL | (None | Some(1 | 2),) => {}
127103
| --------------------- matches all the values already
128104
LL | (Some(1),) => {}
129-
| ^^^^^^^^^^------
130-
| |
131-
| unreachable pattern
132-
| help: remove the match arm
105+
| ^^^^^^^^^^ unreachable pattern
133106

134107
error: unreachable pattern
135108
--> $DIR/exhaustiveness-unreachable-pattern.rs:43:9
@@ -138,21 +111,15 @@ LL | (None | Some(1 | 2),) => {}
138111
| --------------------- matches all the values already
139112
LL | (Some(1),) => {}
140113
LL | (None,) => {}
141-
| ^^^^^^^------
142-
| |
143-
| unreachable pattern
144-
| help: remove the match arm
114+
| ^^^^^^^ unreachable pattern
145115

146116
error: unreachable pattern
147117
--> $DIR/exhaustiveness-unreachable-pattern.rs:48:9
148118
|
149119
LL | ((1 | 2,) | (3 | 4,),) => {}
150120
| ---------------------- matches all the values already
151121
LL | ((1..=4,),) => {}
152-
| ^^^^^^^^^^^------
153-
| |
154-
| unreachable pattern
155-
| help: remove the match arm
122+
| ^^^^^^^^^^^ unreachable pattern
156123

157124
error: unreachable pattern
158125
--> $DIR/exhaustiveness-unreachable-pattern.rs:53:14

tests/ui/pattern/issue-14221.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ LL | A => "A",
1919
| - matches any value
2020
LL |
2121
LL | B => "B",
22-
| ^--------
23-
| |
24-
| unreachable pattern
25-
| help: remove the match arm
22+
| ^ unreachable pattern
2623
|
2724
note: the lint level is defined here
2825
--> $DIR/issue-14221.rs:1:9

tests/ui/pattern/usefulness/consts-opaque.stderr

+9-36
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ error: unreachable pattern
5252
LL | Bar => {}
5353
| --- matches any value
5454
LL | BAR => {}
55-
| ^^^------
56-
| |
57-
| unreachable pattern
58-
| help: remove the match arm
55+
| ^^^ unreachable pattern
5956
|
6057
note: the lint level is defined here
6158
--> $DIR/consts-opaque.rs:6:9
@@ -70,21 +67,15 @@ LL | Bar => {}
7067
| --- matches any value
7168
...
7269
LL | _ => {}
73-
| ^------
74-
| |
75-
| unreachable pattern
76-
| help: remove the match arm
70+
| ^ unreachable pattern
7771

7872
error: unreachable pattern
7973
--> $DIR/consts-opaque.rs:56:9
8074
|
8175
LL | BAR => {}
8276
| --- matches any value
8377
LL | Bar => {}
84-
| ^^^------
85-
| |
86-
| unreachable pattern
87-
| help: remove the match arm
78+
| ^^^ unreachable pattern
8879

8980
error: unreachable pattern
9081
--> $DIR/consts-opaque.rs:58:9
@@ -93,21 +84,15 @@ LL | BAR => {}
9384
| --- matches any value
9485
...
9586
LL | _ => {}
96-
| ^------
97-
| |
98-
| unreachable pattern
99-
| help: remove the match arm
87+
| ^ unreachable pattern
10088

10189
error: unreachable pattern
10290
--> $DIR/consts-opaque.rs:64:9
10391
|
10492
LL | BAR => {}
10593
| --- matches any value
10694
LL | BAR => {} // should not be emitting unreachable warning
107-
| ^^^------
108-
| |
109-
| unreachable pattern
110-
| help: remove the match arm
95+
| ^^^ unreachable pattern
11196

11297
error: unreachable pattern
11398
--> $DIR/consts-opaque.rs:66:9
@@ -116,41 +101,29 @@ LL | BAR => {}
116101
| --- matches any value
117102
...
118103
LL | _ => {} // should not be emitting unreachable warning
119-
| ^------
120-
| |
121-
| unreachable pattern
122-
| help: remove the match arm
104+
| ^ unreachable pattern
123105

124106
error: unreachable pattern
125107
--> $DIR/consts-opaque.rs:72:9
126108
|
127109
LL | BAZ => {}
128110
| --- matches all the values already
129111
LL | Baz::Baz1 => {} // should not be emitting unreachable warning
130-
| ^^^^^^^^^------
131-
| |
132-
| unreachable pattern
133-
| help: remove the match arm
112+
| ^^^^^^^^^ unreachable pattern
134113

135114
error: unreachable pattern
136115
--> $DIR/consts-opaque.rs:79:9
137116
|
138117
LL | Baz::Baz1 => {}
139118
| --------- matches all the values already
140119
LL | BAZ => {}
141-
| ^^^------
142-
| |
143-
| unreachable pattern
144-
| help: remove the match arm
120+
| ^^^ unreachable pattern
145121

146122
error: unreachable pattern
147123
--> $DIR/consts-opaque.rs:87:9
148124
|
149125
LL | _ => {} // should not be emitting unreachable warning
150-
| ^------
151-
| |
152-
| unreachable pattern
153-
| help: remove the match arm
126+
| ^ unreachable pattern
154127
|
155128
note: these patterns collectively make the last one unreachable
156129
--> $DIR/consts-opaque.rs:87:9

tests/ui/pattern/usefulness/empty-types.exhaustive_patterns.stderr

+4-16
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,7 @@ error: unreachable pattern
245245
LL | None => {}
246246
| ---- matches all the values already
247247
LL | _ => {}
248-
| ^------
249-
| |
250-
| unreachable pattern
251-
| help: remove the match arm
248+
| ^ unreachable pattern
252249

253250
error: unreachable pattern
254251
--> $DIR/empty-types.rs:199:13
@@ -445,10 +442,7 @@ error: unreachable pattern
445442
LL | [] => {}
446443
| -- matches all the values already
447444
LL | _ => {}
448-
| ^------
449-
| |
450-
| unreachable pattern
451-
| help: remove the match arm
445+
| ^ unreachable pattern
452446

453447
error[E0004]: non-exhaustive patterns: `[]` not covered
454448
--> $DIR/empty-types.rs:397:11
@@ -491,10 +485,7 @@ LL | None => {}
491485
| ---- matches all the values already
492486
LL | // !useful, !reachable
493487
LL | _ => {}
494-
| ^------
495-
| |
496-
| unreachable pattern
497-
| help: remove the match arm
488+
| ^ unreachable pattern
498489

499490
error: unreachable pattern
500491
--> $DIR/empty-types.rs:431:9
@@ -503,10 +494,7 @@ LL | None => {}
503494
| ---- matches all the values already
504495
LL | // !useful, !reachable
505496
LL | _a => {}
506-
| ^^------
507-
| |
508-
| unreachable pattern
509-
| help: remove the match arm
497+
| ^^ unreachable pattern
510498

511499
error: unreachable pattern
512500
--> $DIR/empty-types.rs:603:9

0 commit comments

Comments
 (0)