Skip to content

Assigning to refutable enum results in invalid fix #157553

@theemathas

Description

@theemathas

Code

enum Foo {
    One,
    Two,
}

fn main() {
    Foo::One = Foo::One;
}

Current output

error[E0005]: refutable pattern in local binding
 --> src/main.rs:7:5
  |
7 |     Foo::One = Foo::One;
  |     ^^^^^^^^ pattern `Foo::Two` not covered
  |
  = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
  = note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html
note: `Foo` defined here
 --> src/main.rs:1:6
  |
1 | enum Foo {
  |      ^^^
2 |     One,
3 |     Two,
  |     --- not covered
  = note: the matched value is of type `Foo`
help: you might want to use `if let` to ignore the variant that isn't matched
  |
7 |     if Foo::One = Foo::One { todo!() };
  |     ++                     +++++++++++

For more information about this error, try `rustc --explain E0005`.

Desired output

Rationale and extra context

  • Foo::One on the left of the = here isn't actually a pattern. It's an (invalid?) assignee expression.
  • The diagnostic says "let bindings require...", but this isn't a let binding.
  • The suggested if Foo::One = Foo::One { todo!() }; is invalid code, since there's no let.

See also rust-lang/reference#2280

Other cases

Rust Version

Reproducible on the playground with version 1.98.0-nightly (2026-06-05 8954863c81df429ebf96)

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))A-patternsRelating to patterns and pattern matchingD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions