-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Add note on non-exhaustiveness when matching on str and nested non-exhaustive enums #115270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #[non_exhaustive] | ||
| pub enum NonExhaustiveEnum { A, B } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") => {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" => {}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // aux-build:non-exhaustive.rs | ||
|
|
||
| extern crate non_exhaustive; | ||
|
|
||
| use non_exhaustive::NonExhaustiveEnum; | ||
|
|
||
| fn main() { | ||
| match Some(NonExhaustiveEnum::A) { | ||
| //~^ ERROR non-exhaustive patterns: `Some(_)` not covered [E0004] | ||
| //~| NOTE pattern `Some(_)` not covered | ||
| //~| NOTE `Option<NonExhaustiveEnum>` defined here | ||
| //~| NOTE the matched value is of type `Option<NonExhaustiveEnum>` | ||
| //~| NOTE `NonExhaustiveEnum` is marked as non-exhaustive | ||
| Some(NonExhaustiveEnum::A) => {} | ||
| Some(NonExhaustiveEnum::B) => {} | ||
| None => {} | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| error[E0004]: non-exhaustive patterns: `Some(_)` not covered | ||
| --> $DIR/nested-non-exhaustive-enums.rs:8:11 | ||
| | | ||
| LL | match Some(NonExhaustiveEnum::A) { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `Some(_)` not covered | ||
| | | ||
| note: `Option<NonExhaustiveEnum>` defined here | ||
| --> $SRC_DIR/core/src/option.rs:LL:COL | ||
| ::: $SRC_DIR/core/src/option.rs:LL:COL | ||
| | | ||
| = note: not covered | ||
| = note: the matched value is of type `Option<NonExhaustiveEnum>` | ||
| = note: `NonExhaustiveEnum` is marked as non-exhaustive | ||
| 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 => {}, | ||
| LL + Some(_) => todo!() | ||
| | | ||
|
|
||
| error: aborting due to previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0004`. |
Uh oh!
There was an error while loading. Please reload this page.