Skip to content

Commit f9567d0

Browse files
committedSep 14, 2024·
Auto merge of rust-lang#128991 - Nadrieril:rustfix-unreachable-pattern, r=compiler-errors
Add a machine-applicable suggestion to "unreachable pattern"
2 parents 4a47e8e + 1f69638 commit f9567d0

19 files changed

+502
-98
lines changed
 

‎compiler/rustc_mir_build/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ mir_build_unreachable_pattern = unreachable pattern
338338
.unreachable_covered_by_catchall = matches any value
339339
.unreachable_covered_by_one = matches all the relevant values
340340
.unreachable_covered_by_many = multiple earlier patterns match some of the same values
341+
.suggestion = remove the match arm
341342
342343
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
343344
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items

‎compiler/rustc_mir_build/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ pub(crate) struct UnreachablePattern<'tcx> {
598598
#[note(mir_build_unreachable_covered_by_many)]
599599
pub(crate) covered_by_many: Option<MultiSpan>,
600600
pub(crate) covered_by_many_n_more_count: usize,
601+
#[suggestion(code = "", applicability = "machine-applicable")]
602+
pub(crate) suggest_remove: Option<Span>,
601603
}
602604

603605
#[derive(Diagnostic)]

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

+30-5
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
413413
// Emit lints in the order in which they occur in the file.
414414
redundant_subpats.sort_unstable_by_key(|(pat, _)| pat.data().span);
415415
for (pat, explanation) in redundant_subpats {
416-
report_unreachable_pattern(cx, arm.arm_data, pat, &explanation)
416+
report_unreachable_pattern(cx, arm.arm_data, pat, &explanation, None)
417417
}
418418
}
419419
}
@@ -474,7 +474,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
474474
hir::MatchSource::ForLoopDesugar
475475
| hir::MatchSource::Postfix
476476
| hir::MatchSource::Normal
477-
| hir::MatchSource::FormatArgs => report_arm_reachability(&cx, &report),
477+
| hir::MatchSource::FormatArgs => {
478+
let is_match_arm =
479+
matches!(source, hir::MatchSource::Postfix | hir::MatchSource::Normal);
480+
report_arm_reachability(&cx, &report, is_match_arm);
481+
}
478482
// Unreachable patterns in try and await expressions occur when one of
479483
// the arms are an uninhabited type. Which is OK.
480484
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar(_) => {}
@@ -626,7 +630,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
626630
) -> Result<RefutableFlag, ErrorGuaranteed> {
627631
let (cx, report) = self.analyze_binding(pat, Refutable, scrut)?;
628632
// Report if the pattern is unreachable, which can only occur when the type is uninhabited.
629-
report_arm_reachability(&cx, &report);
633+
report_arm_reachability(&cx, &report, false);
630634
// If the list of witnesses is empty, the match is exhaustive, i.e. the `if let` pattern is
631635
// irrefutable.
632636
Ok(if report.non_exhaustiveness_witnesses.is_empty() { Irrefutable } else { Refutable })
@@ -916,6 +920,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
916920
hir_id: HirId,
917921
pat: &DeconstructedPat<'p, 'tcx>,
918922
explanation: &RedundancyExplanation<'p, 'tcx>,
923+
whole_arm_span: Option<Span>,
919924
) {
920925
static CAP_COVERED_BY_MANY: usize = 4;
921926
let pat_span = pat.data().span;
@@ -928,13 +933,15 @@ fn report_unreachable_pattern<'p, 'tcx>(
928933
covered_by_one: None,
929934
covered_by_many: None,
930935
covered_by_many_n_more_count: 0,
936+
suggest_remove: None,
931937
};
932938
match explanation.covered_by.as_slice() {
933939
[] => {
934940
// Empty pattern; we report the uninhabited type that caused the emptiness.
935941
lint.span = None; // Don't label the pattern itself
936942
lint.uninhabited_note = Some(()); // Give a link about empty types
937943
lint.matches_no_values = Some(pat_span);
944+
lint.suggest_remove = whole_arm_span; // Suggest to remove the match arm
938945
pat.walk(&mut |subpat| {
939946
let ty = **subpat.ty();
940947
if cx.is_uninhabited(ty) {
@@ -982,10 +989,28 @@ fn report_unreachable_pattern<'p, 'tcx>(
982989
}
983990

984991
/// Report unreachable arms, if any.
985-
fn report_arm_reachability<'p, 'tcx>(cx: &PatCtxt<'p, 'tcx>, report: &UsefulnessReport<'p, 'tcx>) {
992+
fn report_arm_reachability<'p, 'tcx>(
993+
cx: &PatCtxt<'p, 'tcx>,
994+
report: &UsefulnessReport<'p, 'tcx>,
995+
is_match_arm: bool,
996+
) {
997+
let sm = cx.tcx.sess.source_map();
986998
for (arm, is_useful) in report.arm_usefulness.iter() {
987999
if let Usefulness::Redundant(explanation) = is_useful {
988-
report_unreachable_pattern(cx, arm.arm_data, arm.pat, explanation)
1000+
let hir_id = arm.arm_data;
1001+
let arm_span = cx.tcx.hir().span(hir_id);
1002+
let whole_arm_span = if is_match_arm {
1003+
// If the arm is followed by a comma, extend the span to include it.
1004+
let with_whitespace = sm.span_extend_while_whitespace(arm_span);
1005+
if let Some(comma) = sm.span_look_ahead(with_whitespace, ",", Some(1)) {
1006+
Some(arm_span.to(comma))
1007+
} else {
1008+
Some(arm_span)
1009+
}
1010+
} else {
1011+
None
1012+
};
1013+
report_unreachable_pattern(cx, hir_id, arm.pat, explanation, whole_arm_span)
9891014
}
9901015
}
9911016
}

‎tests/ui/pattern/usefulness/empty-match-check-notes.exhaustive_patterns.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/empty-match-check-notes.rs:17:9
33
|
44
LL | _ => {}
5-
| ^ matches no values because `EmptyEnum` is uninhabited
5+
| ^------
6+
| |
7+
| matches no values because `EmptyEnum` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,23 +18,32 @@ error: unreachable pattern
1518
--> $DIR/empty-match-check-notes.rs:22:9
1619
|
1720
LL | _ if false => {}
18-
| ^ matches no values because `EmptyEnum` is uninhabited
21+
| ^---------------
22+
| |
23+
| matches no values because `EmptyEnum` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

2228
error: unreachable pattern
2329
--> $DIR/empty-match-check-notes.rs:31:9
2430
|
2531
LL | _ => {}
26-
| ^ matches no values because `EmptyForeignEnum` is uninhabited
32+
| ^------
33+
| |
34+
| matches no values because `EmptyForeignEnum` is uninhabited
35+
| help: remove the match arm
2736
|
2837
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2938

3039
error: unreachable pattern
3140
--> $DIR/empty-match-check-notes.rs:36:9
3241
|
3342
LL | _ if false => {}
34-
| ^ matches no values because `EmptyForeignEnum` is uninhabited
43+
| ^---------------
44+
| |
45+
| matches no values because `EmptyForeignEnum` is uninhabited
46+
| help: remove the match arm
3547
|
3648
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3749

‎tests/ui/pattern/usefulness/empty-match-check-notes.normal.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/empty-match-check-notes.rs:17:9
33
|
44
LL | _ => {}
5-
| ^ matches no values because `EmptyEnum` is uninhabited
5+
| ^------
6+
| |
7+
| matches no values because `EmptyEnum` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,23 +18,32 @@ error: unreachable pattern
1518
--> $DIR/empty-match-check-notes.rs:22:9
1619
|
1720
LL | _ if false => {}
18-
| ^ matches no values because `EmptyEnum` is uninhabited
21+
| ^---------------
22+
| |
23+
| matches no values because `EmptyEnum` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

2228
error: unreachable pattern
2329
--> $DIR/empty-match-check-notes.rs:31:9
2430
|
2531
LL | _ => {}
26-
| ^ matches no values because `EmptyForeignEnum` is uninhabited
32+
| ^------
33+
| |
34+
| matches no values because `EmptyForeignEnum` is uninhabited
35+
| help: remove the match arm
2736
|
2837
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2938

3039
error: unreachable pattern
3140
--> $DIR/empty-match-check-notes.rs:36:9
3241
|
3342
LL | _ if false => {}
34-
| ^ matches no values because `EmptyForeignEnum` is uninhabited
43+
| ^---------------
44+
| |
45+
| matches no values because `EmptyForeignEnum` is uninhabited
46+
| help: remove the match arm
3547
|
3648
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3749

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

+140-35
Large diffs are not rendered by default.

‎tests/ui/pattern/usefulness/empty-types.never_pats.stderr

+60-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ error: unreachable pattern
1111
--> $DIR/empty-types.rs:49:9
1212
|
1313
LL | _ => {}
14-
| ^ matches no values because `!` is uninhabited
14+
| ^------
15+
| |
16+
| matches no values because `!` is uninhabited
17+
| help: remove the match arm
1518
|
1619
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
1720
note: the lint level is defined here
@@ -24,7 +27,10 @@ error: unreachable pattern
2427
--> $DIR/empty-types.rs:52:9
2528
|
2629
LL | _x => {}
27-
| ^^ matches no values because `!` is uninhabited
30+
| ^^------
31+
| |
32+
| matches no values because `!` is uninhabited
33+
| help: remove the match arm
2834
|
2935
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3036

@@ -47,7 +53,10 @@ error: unreachable pattern
4753
--> $DIR/empty-types.rs:83:9
4854
|
4955
LL | _ => {}
50-
| ^ matches no values because `!` is uninhabited
56+
| ^------
57+
| |
58+
| matches no values because `!` is uninhabited
59+
| help: remove the match arm
5160
|
5261
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
5362

@@ -120,15 +129,21 @@ error: unreachable pattern
120129
--> $DIR/empty-types.rs:132:13
121130
|
122131
LL | _ => {}
123-
| ^ matches no values because `Void` is uninhabited
132+
| ^------
133+
| |
134+
| matches no values because `Void` is uninhabited
135+
| help: remove the match arm
124136
|
125137
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
126138

127139
error: unreachable pattern
128140
--> $DIR/empty-types.rs:135:13
129141
|
130142
LL | _ if false => {}
131-
| ^ matches no values because `Void` is uninhabited
143+
| ^---------------
144+
| |
145+
| matches no values because `Void` is uninhabited
146+
| help: remove the match arm
132147
|
133148
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
134149

@@ -155,47 +170,65 @@ error: unreachable pattern
155170
--> $DIR/empty-types.rs:199:13
156171
|
157172
LL | _ => {}
158-
| ^ matches no values because `!` is uninhabited
173+
| ^------
174+
| |
175+
| matches no values because `!` is uninhabited
176+
| help: remove the match arm
159177
|
160178
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
161179

162180
error: unreachable pattern
163181
--> $DIR/empty-types.rs:204:13
164182
|
165183
LL | _ => {}
166-
| ^ matches no values because `!` is uninhabited
184+
| ^------
185+
| |
186+
| matches no values because `!` is uninhabited
187+
| help: remove the match arm
167188
|
168189
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
169190

170191
error: unreachable pattern
171192
--> $DIR/empty-types.rs:209:13
172193
|
173194
LL | _ => {}
174-
| ^ matches no values because `!` is uninhabited
195+
| ^------
196+
| |
197+
| matches no values because `!` is uninhabited
198+
| help: remove the match arm
175199
|
176200
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
177201

178202
error: unreachable pattern
179203
--> $DIR/empty-types.rs:214:13
180204
|
181205
LL | _ => {}
182-
| ^ matches no values because `!` is uninhabited
206+
| ^------
207+
| |
208+
| matches no values because `!` is uninhabited
209+
| help: remove the match arm
183210
|
184211
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
185212

186213
error: unreachable pattern
187214
--> $DIR/empty-types.rs:220:13
188215
|
189216
LL | _ => {}
190-
| ^ matches no values because `!` is uninhabited
217+
| ^------
218+
| |
219+
| matches no values because `!` is uninhabited
220+
| help: remove the match arm
191221
|
192222
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
193223

194224
error: unreachable pattern
195225
--> $DIR/empty-types.rs:281:9
196226
|
197227
LL | _ => {}
198-
| ^ matches no values because `!` is uninhabited
228+
| ^------
229+
| |
230+
| matches no values because `!` is uninhabited
231+
| help: remove the match arm
199232
|
200233
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
201234

@@ -476,31 +509,43 @@ error: unreachable pattern
476509
--> $DIR/empty-types.rs:603:9
477510
|
478511
LL | _ => {}
479-
| ^ matches no values because `!` is uninhabited
512+
| ^------
513+
| |
514+
| matches no values because `!` is uninhabited
515+
| help: remove the match arm
480516
|
481517
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
482518

483519
error: unreachable pattern
484520
--> $DIR/empty-types.rs:606:9
485521
|
486522
LL | _x => {}
487-
| ^^ matches no values because `!` is uninhabited
523+
| ^^------
524+
| |
525+
| matches no values because `!` is uninhabited
526+
| help: remove the match arm
488527
|
489528
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
490529

491530
error: unreachable pattern
492531
--> $DIR/empty-types.rs:609:9
493532
|
494533
LL | _ if false => {}
495-
| ^ matches no values because `!` is uninhabited
534+
| ^---------------
535+
| |
536+
| matches no values because `!` is uninhabited
537+
| help: remove the match arm
496538
|
497539
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
498540

499541
error: unreachable pattern
500542
--> $DIR/empty-types.rs:612:9
501543
|
502544
LL | _x if false => {}
503-
| ^^ matches no values because `!` is uninhabited
545+
| ^^---------------
546+
| |
547+
| matches no values because `!` is uninhabited
548+
| help: remove the match arm
504549
|
505550
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
506551

‎tests/ui/pattern/usefulness/empty-types.normal.stderr

+60-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/empty-types.rs:49:9
33
|
44
LL | _ => {}
5-
| ^ matches no values because `!` is uninhabited
5+
| ^------
6+
| |
7+
| matches no values because `!` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,7 +18,10 @@ error: unreachable pattern
1518
--> $DIR/empty-types.rs:52:9
1619
|
1720
LL | _x => {}
18-
| ^^ matches no values because `!` is uninhabited
21+
| ^^------
22+
| |
23+
| matches no values because `!` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

@@ -38,7 +44,10 @@ error: unreachable pattern
3844
--> $DIR/empty-types.rs:83:9
3945
|
4046
LL | _ => {}
41-
| ^ matches no values because `!` is uninhabited
47+
| ^------
48+
| |
49+
| matches no values because `!` is uninhabited
50+
| help: remove the match arm
4251
|
4352
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
4453

@@ -111,15 +120,21 @@ error: unreachable pattern
111120
--> $DIR/empty-types.rs:132:13
112121
|
113122
LL | _ => {}
114-
| ^ matches no values because `Void` is uninhabited
123+
| ^------
124+
| |
125+
| matches no values because `Void` is uninhabited
126+
| help: remove the match arm
115127
|
116128
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
117129

118130
error: unreachable pattern
119131
--> $DIR/empty-types.rs:135:13
120132
|
121133
LL | _ if false => {}
122-
| ^ matches no values because `Void` is uninhabited
134+
| ^---------------
135+
| |
136+
| matches no values because `Void` is uninhabited
137+
| help: remove the match arm
123138
|
124139
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
125140

@@ -146,47 +161,65 @@ error: unreachable pattern
146161
--> $DIR/empty-types.rs:199:13
147162
|
148163
LL | _ => {}
149-
| ^ matches no values because `!` is uninhabited
164+
| ^------
165+
| |
166+
| matches no values because `!` is uninhabited
167+
| help: remove the match arm
150168
|
151169
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
152170

153171
error: unreachable pattern
154172
--> $DIR/empty-types.rs:204:13
155173
|
156174
LL | _ => {}
157-
| ^ matches no values because `!` is uninhabited
175+
| ^------
176+
| |
177+
| matches no values because `!` is uninhabited
178+
| help: remove the match arm
158179
|
159180
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
160181

161182
error: unreachable pattern
162183
--> $DIR/empty-types.rs:209:13
163184
|
164185
LL | _ => {}
165-
| ^ matches no values because `!` is uninhabited
186+
| ^------
187+
| |
188+
| matches no values because `!` is uninhabited
189+
| help: remove the match arm
166190
|
167191
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
168192

169193
error: unreachable pattern
170194
--> $DIR/empty-types.rs:214:13
171195
|
172196
LL | _ => {}
173-
| ^ matches no values because `!` is uninhabited
197+
| ^------
198+
| |
199+
| matches no values because `!` is uninhabited
200+
| help: remove the match arm
174201
|
175202
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
176203

177204
error: unreachable pattern
178205
--> $DIR/empty-types.rs:220:13
179206
|
180207
LL | _ => {}
181-
| ^ matches no values because `!` is uninhabited
208+
| ^------
209+
| |
210+
| matches no values because `!` is uninhabited
211+
| help: remove the match arm
182212
|
183213
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
184214

185215
error: unreachable pattern
186216
--> $DIR/empty-types.rs:281:9
187217
|
188218
LL | _ => {}
189-
| ^ matches no values because `!` is uninhabited
219+
| ^------
220+
| |
221+
| matches no values because `!` is uninhabited
222+
| help: remove the match arm
190223
|
191224
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
192225

@@ -467,31 +500,43 @@ error: unreachable pattern
467500
--> $DIR/empty-types.rs:603:9
468501
|
469502
LL | _ => {}
470-
| ^ matches no values because `!` is uninhabited
503+
| ^------
504+
| |
505+
| matches no values because `!` is uninhabited
506+
| help: remove the match arm
471507
|
472508
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
473509

474510
error: unreachable pattern
475511
--> $DIR/empty-types.rs:606:9
476512
|
477513
LL | _x => {}
478-
| ^^ matches no values because `!` is uninhabited
514+
| ^^------
515+
| |
516+
| matches no values because `!` is uninhabited
517+
| help: remove the match arm
479518
|
480519
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
481520

482521
error: unreachable pattern
483522
--> $DIR/empty-types.rs:609:9
484523
|
485524
LL | _ if false => {}
486-
| ^ matches no values because `!` is uninhabited
525+
| ^---------------
526+
| |
527+
| matches no values because `!` is uninhabited
528+
| help: remove the match arm
487529
|
488530
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
489531

490532
error: unreachable pattern
491533
--> $DIR/empty-types.rs:612:9
492534
|
493535
LL | _x if false => {}
494-
| ^^ matches no values because `!` is uninhabited
536+
| ^^---------------
537+
| |
538+
| matches no values because `!` is uninhabited
539+
| help: remove the match arm
495540
|
496541
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
497542

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,32 @@ error: unreachable pattern
5959
--> $DIR/explain-unreachable-pats.rs:52:9
6060
|
6161
LL | Err(_) => {}
62-
| ^^^^^^ matches no values because `!` is uninhabited
62+
| ^^^^^^------
63+
| |
64+
| matches no values because `!` is uninhabited
65+
| help: remove the match arm
6366
|
6467
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
6568

6669
error: unreachable pattern
6770
--> $DIR/explain-unreachable-pats.rs:66:9
6871
|
6972
LL | (Err(_), Err(_)) => {}
70-
| ^^^^^^^^^^^^^^^^ matches no values because `Void2` is uninhabited
73+
| ^^^^^^^^^^^^^^^^------
74+
| |
75+
| matches no values because `Void2` is uninhabited
76+
| help: remove the match arm
7177
|
7278
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
7379

7480
error: unreachable pattern
7581
--> $DIR/explain-unreachable-pats.rs:73:9
7682
|
7783
LL | (Err(_), Err(_)) => {}
78-
| ^^^^^^^^^^^^^^^^ matches no values because `Void1` is uninhabited
84+
| ^^^^^^^^^^^^^^^^------
85+
| |
86+
| matches no values because `Void1` is uninhabited
87+
| help: remove the match arm
7988
|
8089
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
8190

‎tests/ui/pattern/usefulness/impl-trait.stderr

+32-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/impl-trait.rs:17:13
33
|
44
LL | _ => {}
5-
| ^ matches no values because `Void` is uninhabited
5+
| ^------
6+
| |
7+
| matches no values because `Void` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,15 +18,21 @@ error: unreachable pattern
1518
--> $DIR/impl-trait.rs:31:13
1619
|
1720
LL | _ => {}
18-
| ^ matches no values because `Void` is uninhabited
21+
| ^------
22+
| |
23+
| matches no values because `Void` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

2228
error: unreachable pattern
2329
--> $DIR/impl-trait.rs:45:13
2430
|
2531
LL | Some(_) => {}
26-
| ^^^^^^^ matches no values because `Void` is uninhabited
32+
| ^^^^^^^------
33+
| |
34+
| matches no values because `Void` is uninhabited
35+
| help: remove the match arm
2736
|
2837
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2938

@@ -39,7 +48,10 @@ error: unreachable pattern
3948
--> $DIR/impl-trait.rs:59:13
4049
|
4150
LL | Some(_) => {}
42-
| ^^^^^^^ matches no values because `Void` is uninhabited
51+
| ^^^^^^^------
52+
| |
53+
| matches no values because `Void` is uninhabited
54+
| help: remove the match arm
4355
|
4456
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
4557

@@ -55,7 +67,10 @@ error: unreachable pattern
5567
--> $DIR/impl-trait.rs:76:9
5668
|
5769
LL | _ => {}
58-
| ^ matches no values because `Void` is uninhabited
70+
| ^------
71+
| |
72+
| matches no values because `Void` is uninhabited
73+
| help: remove the match arm
5974
|
6075
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
6176

@@ -71,7 +86,10 @@ error: unreachable pattern
7186
--> $DIR/impl-trait.rs:94:13
7287
|
7388
LL | _ => {}
74-
| ^ matches no values because `Void` is uninhabited
89+
| ^------
90+
| |
91+
| matches no values because `Void` is uninhabited
92+
| help: remove the match arm
7593
|
7694
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
7795

@@ -95,15 +113,21 @@ error: unreachable pattern
95113
--> $DIR/impl-trait.rs:138:13
96114
|
97115
LL | _ => {}
98-
| ^ matches no values because `SecretelyVoid` is uninhabited
116+
| ^------
117+
| |
118+
| matches no values because `SecretelyVoid` is uninhabited
119+
| help: remove the match arm
99120
|
100121
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
101122

102123
error: unreachable pattern
103124
--> $DIR/impl-trait.rs:151:13
104125
|
105126
LL | _ => {}
106-
| ^ matches no values because `SecretelyDoubleVoid` is uninhabited
127+
| ^------
128+
| |
129+
| matches no values because `SecretelyDoubleVoid` is uninhabited
130+
| help: remove the match arm
107131
|
108132
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
109133

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
#![feature(never_patterns)]
3+
#![feature(exhaustive_patterns)]
4+
#![allow(incomplete_features)]
5+
#![deny(unreachable_patterns)]
6+
7+
enum Void {}
8+
9+
#[rustfmt::skip]
10+
fn main() {
11+
let res: Result<(), Void> = Ok(());
12+
match res {
13+
Ok(_) => {}
14+
//~ ERROR unreachable
15+
//~ ERROR unreachable
16+
}
17+
18+
match res {
19+
Ok(_x) => {}
20+
//~ ERROR unreachable
21+
//~ ERROR unreachable
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
#![feature(never_patterns)]
3+
#![feature(exhaustive_patterns)]
4+
#![allow(incomplete_features)]
5+
#![deny(unreachable_patterns)]
6+
7+
enum Void {}
8+
9+
#[rustfmt::skip]
10+
fn main() {
11+
let res: Result<(), Void> = Ok(());
12+
match res {
13+
Ok(_) => {}
14+
Err(_) => {} //~ ERROR unreachable
15+
Err(_) => {}, //~ ERROR unreachable
16+
}
17+
18+
match res {
19+
Ok(_x) => {}
20+
Err(!), //~ ERROR unreachable
21+
Err(!) //~ ERROR unreachable
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: unreachable pattern
2+
--> $DIR/rustfix-unreachable-pattern.rs:14:9
3+
|
4+
LL | Err(_) => {}
5+
| ^^^^^^------
6+
| |
7+
| matches no values because `Void` is uninhabited
8+
| help: remove the match arm
9+
|
10+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
11+
note: the lint level is defined here
12+
--> $DIR/rustfix-unreachable-pattern.rs:5:9
13+
|
14+
LL | #![deny(unreachable_patterns)]
15+
| ^^^^^^^^^^^^^^^^^^^^
16+
17+
error: unreachable pattern
18+
--> $DIR/rustfix-unreachable-pattern.rs:15:9
19+
|
20+
LL | Err(_) => {},
21+
| ^^^^^^-------
22+
| |
23+
| matches no values because `Void` is uninhabited
24+
| help: remove the match arm
25+
|
26+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
27+
28+
error: unreachable pattern
29+
--> $DIR/rustfix-unreachable-pattern.rs:20:9
30+
|
31+
LL | Err(!),
32+
| ^^^^^^-
33+
| |
34+
| matches no values because `Void` is uninhabited
35+
| help: remove the match arm
36+
|
37+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
38+
39+
error: unreachable pattern
40+
--> $DIR/rustfix-unreachable-pattern.rs:21:9
41+
|
42+
LL | Err(!)
43+
| ^^^^^^
44+
| |
45+
| matches no values because `Void` is uninhabited
46+
| help: remove the match arm
47+
|
48+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
49+
50+
error: aborting due to 4 previous errors
51+

‎tests/ui/reachable/unreachable-try-pattern.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ warning: unreachable pattern
1717
--> $DIR/unreachable-try-pattern.rs:19:24
1818
|
1919
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
20-
| ^^^^^ matches no values because `!` is uninhabited
20+
| ^^^^^-----------------
21+
| |
22+
| matches no values because `!` is uninhabited
23+
| help: remove the match arm
2124
|
2225
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2326
note: the lint level is defined here
@@ -30,7 +33,10 @@ warning: unreachable pattern
3033
--> $DIR/unreachable-try-pattern.rs:30:40
3134
|
3235
LL | let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
33-
| ^^^^^^ matches no values because `Void` is uninhabited
36+
| ^^^^^^----------
37+
| |
38+
| matches no values because `Void` is uninhabited
39+
| help: remove the match arm
3440
|
3541
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3642

‎tests/ui/rfcs/rfc-0000-never_patterns/unreachable.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/unreachable.rs:15:9
33
|
44
LL | Err(!),
5-
| ^^^^^^ matches no values because `Void` is uninhabited
5+
| ^^^^^^-
6+
| |
7+
| matches no values because `Void` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here

‎tests/ui/rfcs/rfc-2008-non-exhaustive/enum_same_crate_empty_match.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/enum_same_crate_empty_match.rs:28:9
33
|
44
LL | _ => {}
5-
| ^ matches no values because `EmptyNonExhaustiveEnum` is uninhabited
5+
| ^------
6+
| |
7+
| matches no values because `EmptyNonExhaustiveEnum` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here

‎tests/ui/rfcs/rfc-2008-non-exhaustive/uninhabited/patterns.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/patterns.rs:42:9
33
|
44
LL | Some(_x) => (),
5-
| ^^^^^^^^ matches no values because `UninhabitedVariants` is uninhabited
5+
| ^^^^^^^^-------
6+
| |
7+
| matches no values because `UninhabitedVariants` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here

‎tests/ui/rfcs/rfc-2008-non-exhaustive/uninhabited/patterns_same_crate.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/patterns_same_crate.rs:53:9
33
|
44
LL | Some(_x) => (),
5-
| ^^^^^^^^ matches no values because `UninhabitedEnum` is uninhabited
5+
| ^^^^^^^^-------
6+
| |
7+
| matches no values because `UninhabitedEnum` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,7 +18,10 @@ error: unreachable pattern
1518
--> $DIR/patterns_same_crate.rs:58:9
1619
|
1720
LL | Some(_x) => (),
18-
| ^^^^^^^^ matches no values because `UninhabitedVariants` is uninhabited
21+
| ^^^^^^^^-------
22+
| |
23+
| matches no values because `UninhabitedVariants` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

‎tests/ui/uninhabited/uninhabited-patterns.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: unreachable pattern
22
--> $DIR/uninhabited-patterns.rs:30:9
33
|
44
LL | Ok(box _) => (),
5-
| ^^^^^^^^^ matches no values because `NotSoSecretlyEmpty` is uninhabited
5+
| ^^^^^^^^^-------
6+
| |
7+
| matches no values because `NotSoSecretlyEmpty` is uninhabited
8+
| help: remove the match arm
69
|
710
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
811
note: the lint level is defined here
@@ -15,7 +18,10 @@ error: unreachable pattern
1518
--> $DIR/uninhabited-patterns.rs:39:9
1619
|
1720
LL | Err(Ok(_y)) => (),
18-
| ^^^^^^^^^^^ matches no values because `NotSoSecretlyEmpty` is uninhabited
21+
| ^^^^^^^^^^^-------
22+
| |
23+
| matches no values because `NotSoSecretlyEmpty` is uninhabited
24+
| help: remove the match arm
1925
|
2026
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2127

0 commit comments

Comments
 (0)
Please sign in to comment.