Skip to content

Bad suggestion for refutable let without equals #123844

Closed
@CAD97

Description

@CAD97

Code

fn main() {
    let Some(x);
    x = 1;
}

Current output

error[E0005]: refutable pattern in local binding
 --> src/main.rs:2:9
  |
2 |     let Some(x);
  |         ^^^^^^^ pattern `None` 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/ch18-02-refutability.html
  = note: the matched value is of type `Option<i32>`
help: you might want to use `let else` to handle the variant that isn't matched
  |
2 |     let Some(x); else { todo!() }
  |                  ++++++++++++++++

Desired output

error[E0005]: refutable pattern in local binding
 --> src/main.rs:2:9
  |
2 |     let Some(x);
  |         ^^^^^^^ pattern `None` 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/ch18-02-refutability.html
  = note: the matched value is of type `Option<i32>`

Rationale and extra context

Just drop the help suggestion, as let-else can't be used with deferred initialization let.

Other cases

No response

Rust Version

1.79.0-nightly
(2024-04-11 a07f3eb43acc5df851e1)

Anything else?

@rustbot label +D-invalid-suggestion

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`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

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions