Skip to content

Commit 6f45f73

Browse files
committed
Change wording of suggestion to add missing match arm
1 parent ab4feea commit 6f45f73

File tree

60 files changed

+290
-271
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

+290
-271
lines changed

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn check_for_bindings_named_same_as_variants(
336336
let ty_path = cx.tcx.def_path_str(edef.did);
337337
let mut err = lint.build(&format!(
338338
"pattern binding `{}` is named the same as one \
339-
of the variants of the type `{}`",
339+
of the variants of the type `{}`",
340340
ident, ty_path
341341
));
342342
err.code(error_code!(E0170));
@@ -508,6 +508,7 @@ fn non_exhaustive_match<'p, 'tcx>(
508508
// informative.
509509
let mut err;
510510
let pattern;
511+
let mut patterns_len = 0;
511512
if is_empty_match && !non_empty_enum {
512513
err = create_e0004(
513514
cx.tcx.sess,
@@ -523,6 +524,7 @@ fn non_exhaustive_match<'p, 'tcx>(
523524
format!("non-exhaustive patterns: {} not covered", joined_patterns),
524525
);
525526
err.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
527+
patterns_len = witnesses.len();
526528
pattern = if witnesses.len() < 4 {
527529
witnesses
528530
.iter()
@@ -622,12 +624,29 @@ fn non_exhaustive_match<'p, 'tcx>(
622624
_ => {}
623625
}
624626

625-
let msg = "ensure that all possible cases are being handled, possibly by adding wildcards \
626-
or more match arms";
627+
let msg = format!(
628+
"ensure that all possible cases are being handled by adding a match arm with a wildcard \
629+
pattern{}{}",
630+
if patterns_len > 1 && patterns_len < 4 && suggestion.is_some() {
631+
", a match arm with multiple or-patterns"
632+
} else {
633+
// we are either not suggesting anything, or suggesting `_`
634+
""
635+
},
636+
match patterns_len {
637+
// non-exhaustive enum case
638+
0 if suggestion.is_some() => " as shown",
639+
0 => "",
640+
1 if suggestion.is_some() => " or an explicit pattern as shown",
641+
1 => " or an explicit pattern",
642+
_ if suggestion.is_some() => " as shown, or multiple match arms",
643+
_ => " or multiple match arms",
644+
},
645+
);
627646
if let Some((span, sugg)) = suggestion {
628-
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
647+
err.span_suggestion_verbose(span, &msg, sugg, Applicability::HasPlaceholders);
629648
} else {
630-
err.help(msg);
649+
err.help(&msg);
631650
}
632651
err.emit();
633652
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: `Opcode` defined here
1010
LL | pub struct Opcode(pub u8);
1111
| ^^^^^^
1212
= note: the matched value is of type `Opcode`
13-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
1414
|
1515
LL ~ Opcode::OP1 => unimplemented!(),
1616
LL ~ Opcode(0_u8) | Opcode(2_u8..=u8::MAX) => todo!(),
@@ -28,7 +28,7 @@ note: `Opcode2` defined here
2828
LL | pub struct Opcode2(Opcode);
2929
| ^^^^^^^
3030
= note: the matched value is of type `Opcode2`
31-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
31+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
3232
|
3333
LL ~ Opcode2::OP2=> unimplemented!(),
3434
LL ~ Opcode2(Opcode(0_u8)) | Opcode2(Opcode(2_u8..=u8::MAX)) => todo!(),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: `L1` defined here
1010
LL | enum L1 { A, B }
1111
| -- ^ not covered
1212
= note: the matched value is of type `L1`
13-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
1515
LL | let _b = || { match l1 { L1::A => (), B => todo!() } };
1616
| ++++++++++++++
@@ -27,7 +27,7 @@ note: `E1` defined here
2727
LL | pub enum E1 {}
2828
| ^^^^^^^^^^^^^^
2929
= note: the matched value is of type `E1`, which is marked as non-exhaustive
30-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
30+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
3131
|
3232
LL ~ let _d = || { match e1 {
3333
LL + _ => todo!(),
@@ -46,7 +46,7 @@ note: `E2` defined here
4646
LL | pub enum E2 { A, B }
4747
| ^^^^^^^^^^^^^^^^^^^^
4848
= note: the matched value is of type `E2`, which is marked as non-exhaustive
49-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
49+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
5050
|
5151
LL | let _e = || { match e2 { E2::A => (), E2::B => (), _ => todo!() } };
5252
| ++++++++++++++

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let c1 = || match x { };
55
| ^
66
|
77
= 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
8+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
99
|
1010
LL ~ let c1 = || match x {
1111
LL + _ => todo!(),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
1919
LL | | }
2020
| |_-
2121
= note: the matched value is of type `Option<i32>`
22-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
22+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
2323
|
2424
LL ~ match x {
2525
LL + None | Some(_) => todo!(),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | enum Terminator {
1212
LL | HastaLaVistaBaby,
1313
| ^^^^^^^^^^^^^^^^ not covered
1414
= 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
15+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1616
|
1717
LL ~ Terminator::TalkToMyHand => {}
1818
LL + HastaLaVistaBaby => todo!()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | match 0usize {
77
= note: the matched value is of type `usize`
88
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
99
= 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
10+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1111
|
1212
LL ~ 0..=usize::MAX => {}
1313
LL + _ => todo!()
@@ -22,7 +22,7 @@ LL | match 0isize {
2222
= note: the matched value is of type `isize`
2323
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
2424
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
25-
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
25+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
2626
|
2727
LL ~ isize::MIN..=isize::MAX => {}
2828
LL + _ => todo!()

0 commit comments

Comments
 (0)