Skip to content

Commit

Permalink
Change wording of suggestion to add missing match arm
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 8, 2022
1 parent ab4feea commit 6f45f73
Show file tree
Hide file tree
Showing 60 changed files with 290 additions and 271 deletions.
29 changes: 24 additions & 5 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Expand Up @@ -336,7 +336,7 @@ fn check_for_bindings_named_same_as_variants(
let ty_path = cx.tcx.def_path_str(edef.did);
let mut err = lint.build(&format!(
"pattern binding `{}` is named the same as one \
of the variants of the type `{}`",
of the variants of the type `{}`",
ident, ty_path
));
err.code(error_code!(E0170));
Expand Down Expand Up @@ -508,6 +508,7 @@ fn non_exhaustive_match<'p, 'tcx>(
// informative.
let mut err;
let pattern;
let mut patterns_len = 0;
if is_empty_match && !non_empty_enum {
err = create_e0004(
cx.tcx.sess,
Expand All @@ -523,6 +524,7 @@ fn non_exhaustive_match<'p, 'tcx>(
format!("non-exhaustive patterns: {} not covered", joined_patterns),
);
err.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
patterns_len = witnesses.len();
pattern = if witnesses.len() < 4 {
witnesses
.iter()
Expand Down Expand Up @@ -622,12 +624,29 @@ fn non_exhaustive_match<'p, 'tcx>(
_ => {}
}

let msg = "ensure that all possible cases are being handled, possibly by adding wildcards \
or more match arms";
let msg = format!(
"ensure that all possible cases are being handled by adding a match arm with a wildcard \
pattern{}{}",
if patterns_len > 1 && patterns_len < 4 && suggestion.is_some() {
", a match arm with multiple or-patterns"
} else {
// we are either not suggesting anything, or suggesting `_`
""
},
match patterns_len {
// non-exhaustive enum case
0 if suggestion.is_some() => " as shown",
0 => "",
1 if suggestion.is_some() => " or an explicit pattern as shown",
1 => " or an explicit pattern",
_ if suggestion.is_some() => " as shown, or multiple match arms",
_ => " or multiple match arms",
},
);
if let Some((span, sugg)) = suggestion {
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
err.span_suggestion_verbose(span, &msg, sugg, Applicability::HasPlaceholders);
} else {
err.help(msg);
err.help(&msg);
}
err.emit();
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ note: `Opcode` defined here
LL | pub struct Opcode(pub u8);
| ^^^^^^
= note: the matched value is of type `Opcode`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
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
|
LL ~ Opcode::OP1 => unimplemented!(),
LL ~ Opcode(0_u8) | Opcode(2_u8..=u8::MAX) => todo!(),
Expand All @@ -28,7 +28,7 @@ note: `Opcode2` defined here
LL | pub struct Opcode2(Opcode);
| ^^^^^^^
= note: the matched value is of type `Opcode2`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
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
|
LL ~ Opcode2::OP2=> unimplemented!(),
LL ~ Opcode2(Opcode(0_u8)) | Opcode2(Opcode(2_u8..=u8::MAX)) => todo!(),
Expand Down
Expand Up @@ -10,7 +10,7 @@ note: `L1` defined here
LL | enum L1 { A, B }
| -- ^ not covered
= note: the matched value is of type `L1`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | let _b = || { match l1 { L1::A => (), B => todo!() } };
| ++++++++++++++
Expand All @@ -27,7 +27,7 @@ note: `E1` defined here
LL | pub enum E1 {}
| ^^^^^^^^^^^^^^
= note: the matched value is of type `E1`, which is marked as non-exhaustive
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ let _d = || { match e1 {
LL + _ => todo!(),
Expand All @@ -46,7 +46,7 @@ note: `E2` defined here
LL | pub enum E2 { A, B }
| ^^^^^^^^^^^^^^^^^^^^
= note: the matched value is of type `E2`, which is marked as non-exhaustive
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL | let _e = || { match e2 { E2::A => (), E2::B => (), _ => todo!() } };
| ++++++++++++++
Expand Down
Expand Up @@ -5,7 +5,7 @@ LL | let c1 = || match x { };
| ^
|
= note: the matched value is of type `u8`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ let c1 = || match x {
LL + _ => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0004-2.stderr
Expand Up @@ -19,7 +19,7 @@ LL | | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
LL | | }
| |_-
= note: the matched value is of type `Option<i32>`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
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
|
LL ~ match x {
LL + None | Some(_) => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0004.stderr
Expand Up @@ -12,7 +12,7 @@ LL | enum Terminator {
LL | HastaLaVistaBaby,
| ^^^^^^^^^^^^^^^^ not covered
= note: the matched value is of type `Terminator`
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ Terminator::TalkToMyHand => {}
LL + HastaLaVistaBaby => todo!()
Expand Down
Expand Up @@ -7,7 +7,7 @@ LL | match 0usize {
= note: the matched value is of type `usize`
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ 0..=usize::MAX => {}
LL + _ => todo!()
Expand All @@ -22,7 +22,7 @@ LL | match 0isize {
= note: the matched value is of type `isize`
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ isize::MIN..=isize::MAX => {}
LL + _ => todo!()
Expand Down

0 comments on commit 6f45f73

Please sign in to comment.