Skip to content

Commit a293619

Browse files
committed
Add note that str cannot be matched exhaustively
1 parent f320f42 commit a293619

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -735,17 +735,21 @@ fn non_exhaustive_match<'p, 'tcx>(
735735
collect_non_exhaustive_tys(&witnesses[0], &mut non_exhaustive_tys);
736736

737737
for ty in non_exhaustive_tys {
738-
if ty == cx.tcx.types.usize || ty == cx.tcx.types.isize {
738+
if ty.is_ptr_sized_integral() {
739739
err.note(format!(
740740
"`{ty}` does not have a fixed maximum value, so a wildcard `_` is necessary to match \
741-
exhaustively",
742-
));
741+
exhaustively",
742+
));
743743
if cx.tcx.sess.is_nightly_build() {
744744
err.help(format!(
745-
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
746-
enable precise `{ty}` matching",
747-
));
745+
"add `#![feature(precise_pointer_size_matching)]` to the crate attributes to \
746+
enable precise `{ty}` matching",
747+
));
748748
}
749+
} else if ty == cx.tcx.types.str_ {
750+
err.note(format!(
751+
"`{ty}` cannot be matched exhaustively, so a wildcard `_` is necessary",
752+
));
749753
}
750754
}
751755
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
let a = "";
3+
let b = "";
4+
match (a, b) {
5+
//~^ ERROR non-exhaustive patterns: `(&_, _)` not covered [E0004]
6+
//~| NOTE pattern `(&_, _)` not covered
7+
//~| NOTE the matched value is of type `(&str, &str)`
8+
//~| NOTE `str` cannot be matched exhaustively, so a wildcard `_` is necessary
9+
("a", "b") => {}
10+
("c", "d") => {}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0004]: non-exhaustive patterns: `(&_, _)` not covered
2+
--> $DIR/issue-105479-str-non-exhaustiveness.rs:4:11
3+
|
4+
LL | match (a, b) {
5+
| ^^^^^^ pattern `(&_, _)` not covered
6+
|
7+
= note: the matched value is of type `(&str, &str)`
8+
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
9+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
10+
|
11+
LL ~ ("c", "d") => {},
12+
LL + (&_, _) => todo!()
13+
|
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0004`.

tests/ui/pattern/usefulness/issue-30240.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | match "world" {
55
| ^^^^^^^ pattern `&_` not covered
66
|
77
= note: the matched value is of type `&str`
8+
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
89
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
910
|
1011
LL ~ "hello" => {},
@@ -18,6 +19,7 @@ LL | match "world" {
1819
| ^^^^^^^ pattern `&_` not covered
1920
|
2021
= note: the matched value is of type `&str`
22+
= note: `str` cannot be matched exhaustively, so a wildcard `_` is necessary
2123
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
2224
|
2325
LL ~ "hello" => {},

0 commit comments

Comments
 (0)