Improve clarity of diagnostic message on non-exhaustive matches

This commit is contained in:
Sebastian Toh 2023-09-03 19:55:11 +08:00
parent 43dd8613a3
commit d87b87d10e
8 changed files with 10 additions and 10 deletions

View File

@ -741,10 +741,10 @@ fn non_exhaustive_match<'p, 'tcx>(
}
} else if ty == cx.tcx.types.str_ {
err.note(format!(
"`{ty}` cannot be matched exhaustively, so a wildcard `_` is necessary",
"`&str` cannot be matched exhaustively, so a wildcard `_` is necessary",
));
} else if cx.is_foreign_non_exhaustive_enum(ty) {
err.note(format!("`{ty}` is marked as non-exhaustive"));
err.note(format!("`{ty}` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively"));
}
}
}

View File

@ -46,7 +46,7 @@ note: `E2` defined here
LL | pub enum E2 { A, B }
| ^^^^^^^^^^^
= note: the matched value is of type `E2`
= note: `E2` is marked as non-exhaustive
= note: `E2` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively
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!() } };

View File

@ -46,7 +46,7 @@ note: `E2` defined here
LL | pub enum E2 { A, B }
| ^^^^^^^^^^^
= note: the matched value is of type `E2`
= note: `E2` is marked as non-exhaustive
= note: `E2` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively
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 | match e2 { E2::A => (), E2::B => (), _ => todo!() };

View File

@ -5,7 +5,7 @@ fn main() {
//~^ ERROR non-exhaustive patterns: `(&_, _)` not covered [E0004]
//~| NOTE pattern `(&_, _)` not covered
//~| NOTE the matched value is of type `(&str, &str)`
//~| NOTE `str` cannot be matched exhaustively, so a wildcard `_` is necessary
//~| NOTE `&str` cannot be matched exhaustively, so a wildcard `_` is necessary
("a", "b") => {}
("c", "d") => {}
}

View File

@ -5,7 +5,7 @@ LL | match (a, b) {
| ^^^^^^ pattern `(&_, _)` not covered
|
= note: the matched value is of type `(&str, &str)`
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
= note: `&str` cannot be matched exhaustively, so a wildcard `_` is necessary
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 ~ ("c", "d") => {},

View File

@ -5,7 +5,7 @@ LL | match "world" {
| ^^^^^^^ pattern `&_` not covered
|
= note: the matched value is of type `&str`
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
= note: `&str` cannot be matched exhaustively, so a wildcard `_` is necessary
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 ~ "hello" => {},
@ -19,7 +19,7 @@ LL | match "world" {
| ^^^^^^^ pattern `&_` not covered
|
= note: the matched value is of type `&str`
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
= note: `&str` cannot be matched exhaustively, so a wildcard `_` is necessary
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 ~ "hello" => {},

View File

@ -10,7 +10,7 @@ note: `Option<NonExhaustiveEnum>` defined here
|
= note: not covered
= note: the matched value is of type `Option<NonExhaustiveEnum>`
= note: `NonExhaustiveEnum` is marked as non-exhaustive
= note: `NonExhaustiveEnum` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively
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 ~ None => {},

View File

@ -29,7 +29,7 @@ note: `NonExhaustiveEnum` defined here
LL | pub enum NonExhaustiveEnum {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: the matched value is of type `NonExhaustiveEnum`
= note: `NonExhaustiveEnum` is marked as non-exhaustive
= note: `NonExhaustiveEnum` is marked as non-exhaustive, so a wildcard `_` is necessary to match exhaustively
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 ~ NonExhaustiveEnum::Struct { .. } => "third",