Add note that str cannot be matched exhaustively

This commit is contained in:
Sebastian Toh 2023-08-27 20:14:56 +08:00
parent f320f42c59
commit a293619caa
4 changed files with 41 additions and 6 deletions

View File

@ -735,17 +735,21 @@ fn non_exhaustive_match<'p, 'tcx>(
collect_non_exhaustive_tys(&witnesses[0], &mut non_exhaustive_tys);
for ty in non_exhaustive_tys {
if ty == cx.tcx.types.usize || ty == cx.tcx.types.isize {
if ty.is_ptr_sized_integral() {
err.note(format!(
"`{ty}` does not have a fixed maximum value, so a wildcard `_` is necessary to match \
exhaustively",
));
exhaustively",
));
if cx.tcx.sess.is_nightly_build() {
err.help(format!(
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
enable precise `{ty}` matching",
));
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
enable precise `{ty}` matching",
));
}
} else if ty == cx.tcx.types.str_ {
err.note(format!(
"`{ty}` cannot be matched exhaustively, so a wildcard `_` is necessary",
));
}
}
}

View File

@ -0,0 +1,12 @@
fn main() {
let a = "";
let b = "";
match (a, b) {
//~^ 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
("a", "b") => {}
("c", "d") => {}
}
}

View File

@ -0,0 +1,17 @@
error[E0004]: non-exhaustive patterns: `(&_, _)` not covered
--> $DIR/issue-105479-str-non-exhaustiveness.rs:4:11
|
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
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") => {},
LL + (&_, _) => todo!()
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0004`.

View File

@ -5,6 +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
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" => {},
@ -18,6 +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
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" => {},