Skip to content

Commit e1458ee

Browse files
committed
When encountering a match expr with no arms, suggest it
Given ```rust match Some(42) {} ``` suggest ```rust match Some(42) { None | Some(_) => todo!(), } ```
1 parent c5ecc15 commit e1458ee

File tree

60 files changed

+525
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+525
-278
lines changed

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

+54-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, '_, 'tcx> {
6363
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
6464
intravisit::walk_expr(self, ex);
6565
match &ex.kind {
66-
hir::ExprKind::Match(scrut, arms, source) => self.check_match(scrut, arms, *source),
66+
hir::ExprKind::Match(scrut, arms, source) => {
67+
self.check_match(scrut, arms, *source, ex.span)
68+
}
6769
hir::ExprKind::Let(pat, scrut, span) => self.check_let(pat, scrut, *span),
6870
_ => {}
6971
}
@@ -160,6 +162,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
160162
scrut: &hir::Expr<'_>,
161163
hir_arms: &'tcx [hir::Arm<'tcx>],
162164
source: hir::MatchSource,
165+
expr_span: Span,
163166
) {
164167
let mut cx = self.new_cx(scrut.hir_id);
165168

@@ -205,15 +208,14 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
205208
}
206209

207210
// Check if the match is exhaustive.
208-
let is_empty_match = arms.is_empty();
209211
let witnesses = report.non_exhaustiveness_witnesses;
210212
if !witnesses.is_empty() {
211213
if source == hir::MatchSource::ForLoopDesugar && hir_arms.len() == 2 {
212214
// the for loop pattern is not irrefutable
213215
let pat = hir_arms[1].pat.for_loop_some().unwrap();
214216
self.check_irrefutable(pat, "`for` loop binding", None);
215217
} else {
216-
non_exhaustive_match(&cx, scrut_ty, scrut.span, witnesses, is_empty_match);
218+
non_exhaustive_match(&cx, scrut_ty, scrut.span, witnesses, hir_arms, expr_span);
217219
}
218220
}
219221
}
@@ -496,21 +498,25 @@ fn non_exhaustive_match<'p, 'tcx>(
496498
scrut_ty: Ty<'tcx>,
497499
sp: Span,
498500
witnesses: Vec<DeconstructedPat<'p, 'tcx>>,
499-
is_empty_match: bool,
501+
arms: &[hir::Arm<'tcx>],
502+
expr_span: Span,
500503
) {
504+
let is_empty_match = arms.is_empty();
501505
let non_empty_enum = match scrut_ty.kind() {
502506
ty::Adt(def, _) => def.is_enum() && !def.variants.is_empty(),
503507
_ => false,
504508
};
505509
// In the case of an empty match, replace the '`_` not covered' diagnostic with something more
506510
// informative.
507511
let mut err;
512+
let pattern;
508513
if is_empty_match && !non_empty_enum {
509514
err = create_e0004(
510515
cx.tcx.sess,
511516
sp,
512517
format!("non-exhaustive patterns: type `{}` is non-empty", scrut_ty),
513518
);
519+
pattern = "_".to_string();
514520
} else {
515521
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
516522
err = create_e0004(
@@ -519,6 +525,15 @@ fn non_exhaustive_match<'p, 'tcx>(
519525
format!("non-exhaustive patterns: {} not covered", joined_patterns),
520526
);
521527
err.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
528+
pattern = if witnesses.len() < 4 {
529+
witnesses
530+
.iter()
531+
.map(|witness| witness.to_pat(cx).to_string())
532+
.collect::<Vec<String>>()
533+
.join(" | ")
534+
} else {
535+
"_".to_string()
536+
};
522537
};
523538

524539
let is_variant_list_non_exhaustive = match scrut_ty.kind() {
@@ -527,10 +542,6 @@ fn non_exhaustive_match<'p, 'tcx>(
527542
};
528543

529544
adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
530-
err.help(
531-
"ensure that all possible cases are being handled, \
532-
possibly by adding wildcards or more match arms",
533-
);
534545
err.note(&format!(
535546
"the matched value is of type `{}`{}",
536547
scrut_ty,
@@ -542,14 +553,14 @@ fn non_exhaustive_match<'p, 'tcx>(
542553
&& matches!(witnesses[0].ctor(), Constructor::NonExhaustive)
543554
{
544555
err.note(&format!(
545-
"`{}` does not have a fixed maximum value, \
546-
so a wildcard `_` is necessary to match exhaustively",
556+
"`{}` does not have a fixed maximum value, so a wildcard `_` is necessary to match \
557+
exhaustively",
547558
scrut_ty,
548559
));
549560
if cx.tcx.sess.is_nightly_build() {
550561
err.help(&format!(
551-
"add `#![feature(precise_pointer_size_matching)]` \
552-
to the crate attributes to enable precise `{}` matching",
562+
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
563+
enable precise `{}` matching",
553564
scrut_ty,
554565
));
555566
}
@@ -559,6 +570,37 @@ fn non_exhaustive_match<'p, 'tcx>(
559570
err.note("references are always considered inhabited");
560571
}
561572
}
573+
574+
let mut suggestion = None;
575+
let sm = cx.tcx.sess.source_map();
576+
match arms {
577+
[] if sp.ctxt() == expr_span.ctxt() => {
578+
// Get the span for the empty match body `{}`.
579+
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
580+
(format!("\n{}", snippet), " ")
581+
} else {
582+
(" ".to_string(), "")
583+
};
584+
suggestion = Some((
585+
sp.shrink_to_hi().with_hi(expr_span.hi()),
586+
format!(
587+
" {{{indentation}{more}{pattern} => todo!(),{indentation}}}",
588+
indentation = indentation,
589+
more = more,
590+
pattern = pattern,
591+
),
592+
));
593+
}
594+
_ => {}
595+
}
596+
597+
let msg = "ensure that all possible cases are being handled, possibly by adding wildcards \
598+
or more match arms";
599+
if let Some((span, sugg)) = suggestion {
600+
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
601+
} else {
602+
err.help(msg);
603+
}
562604
err.emit();
563605
}
564606

src/test/ui/closures/2229_closure_analysis/match/issue-88331.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | pub struct Opcode(pub u8);
77
LL | move |i| match msg_type {
88
| ^^^^^^^^ patterns `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
99
|
10-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1110
= note: the matched value is of type `Opcode`
11+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1212

1313
error[E0004]: non-exhaustive patterns: `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
1414
--> $DIR/issue-88331.rs:27:20
@@ -19,8 +19,8 @@ LL | pub struct Opcode2(Opcode);
1919
LL | move |i| match msg_type {
2020
| ^^^^^^^^ patterns `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
2121
|
22-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2322
= note: the matched value is of type `Opcode2`
23+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2424

2525
error: aborting due to 2 previous errors
2626

src/test/ui/closures/2229_closure_analysis/match/non-exhaustive-match.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,31 @@ LL | enum L1 { A, B }
1010
LL | let _b = || { match l1 { L1::A => () } };
1111
| ^^ pattern `B` not covered
1212
|
13-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1413
= note: the matched value is of type `L1`
14+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1515

1616
error[E0004]: non-exhaustive patterns: type `E1` is non-empty
1717
--> $DIR/non-exhaustive-match.rs:37:25
1818
|
1919
LL | let _d = || { match e1 {} };
2020
| ^^
2121
|
22-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2322
= note: the matched value is of type `E1`, which is marked as non-exhaustive
23+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
24+
|
25+
LL ~ let _d = || { match e1 {
26+
LL + _ => todo!(),
27+
LL ~ } };
28+
|
2429

2530
error[E0004]: non-exhaustive patterns: `_` not covered
2631
--> $DIR/non-exhaustive-match.rs:39:25
2732
|
2833
LL | let _e = || { match e2 { E2::A => (), E2::B => () } };
2934
| ^^ pattern `_` not covered
3035
|
31-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3236
= note: the matched value is of type `E2`, which is marked as non-exhaustive
37+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3338

3439
error[E0505]: cannot move out of `e3` because it is borrowed
3540
--> $DIR/non-exhaustive-match.rs:46:22

src/test/ui/closures/2229_closure_analysis/match/pattern-matching-should-fail.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ error[E0004]: non-exhaustive patterns: type `u8` is non-empty
44
LL | let c1 = || match x { };
55
| ^
66
|
7-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
87
= note: the matched value is of type `u8`
8+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
9+
|
10+
LL ~ let c1 = || match x {
11+
LL + _ => todo!(),
12+
LL ~ };
13+
|
914

1015
error[E0381]: use of possibly-uninitialized variable: `x`
1116
--> $DIR/pattern-matching-should-fail.rs:8:23

src/test/ui/error-codes/E0004-2.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ LL | None,
1212
LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
1313
| ---- not covered
1414
|
15-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1615
= note: the matched value is of type `Option<i32>`
16+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
17+
|
18+
LL ~ match x {
19+
LL + None | Some(_) => todo!(),
20+
LL ~ }
21+
|
1722

1823
error: aborting due to previous error
1924

src/test/ui/error-codes/E0004.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | | }
1111
LL | match x {
1212
| ^ pattern `HastaLaVistaBaby` not covered
1313
|
14-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1514
= note: the matched value is of type `Terminator`
15+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

1717
error: aborting due to previous error
1818

src/test/ui/feature-gates/feature-gate-precise_pointer_size_matching.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ error[E0004]: non-exhaustive patterns: `_` not covered
44
LL | match 0usize {
55
| ^^^^^^ pattern `_` not covered
66
|
7-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
87
= note: the matched value is of type `usize`
98
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
109
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
10+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1111

1212
error[E0004]: non-exhaustive patterns: `_` not covered
1313
--> $DIR/feature-gate-precise_pointer_size_matching.rs:10:11
1414
|
1515
LL | match 0isize {
1616
| ^^^^^^ pattern `_` not covered
1717
|
18-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1918
= note: the matched value is of type `isize`
2019
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
2120
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
21+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2222

2323
error: aborting due to 2 previous errors
2424

0 commit comments

Comments
 (0)