Skip to content

mutually exclusive match arms with if cases are not recognized to be covering #93611

Open
@drahnr

Description

@drahnr

I tried this code:

enum Foo {
    A(bool),
    B,
    C,
}


fn main() {

    let a = Foo::A(false);
    match a {
        Foo::A(x) if x => {},
        Foo::A(x) if !x => {},
        Foo::B => {},
        Foo::C => {},
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b70f6933b22baa139e9ad679a7559935

I expected to see this happen: Compilation without an error

Instead, this happened: Rustc wants a catch-all for Foo::A(_), which is not required since the other match arms are covering.

Compiling playground v0.0.1 (/playground)
error[[E0004]](https://doc.rust-lang.org/stable/error-index.html#E0004): non-exhaustive patterns: `A(_)` not covered
  [--> src/main.rs:12:11
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b70f6933b22baa139e9ad679a7559935#)   |
2  | / enum Foo {
3  | |     A(bool),
   | |     - not covered
4  | |     B,
5  | |     C,
6  | | }
   | |_- `Foo` defined here
...
12 |       match a {
   |             ^ pattern `A(_)` not covered
   |
   = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
   = note: the matched value is of type `Foo`

For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground` due to previous error

Meta

rustc --version --verbose:

1.58.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions