rust-lang / rust-lang/rust

`let else` not suggested for refutable let patterns.

Open
#122,404 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let foo = 0;
    let 1 = foo;
}
Current output
error[E0005]: refutable pattern in local binding
 --> src/main.rs:3:9
  |
3 |     let 1 = foo;
  |         ^ patterns `i32::MIN..=0_i32` and `2_i32..=i32::MAX` 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 `i32`
help: you might want to use `if let` to ignore the variants that aren't matched
  |
3 |     if let 1 = foo { todo!() };
  |     ++             +++++++++++
help: alternatively, you could prepend the pattern with an underscore to define a new named variable; identifiers cannot begin with digits
  |
3 |     let _1 = foo;
  |         +

For more information about this error, try `rustc --explain E0005`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
help: you might want to use `let else`
  |
3 |     let 1 = foo else { todo!() };
  |                 ++++++++++++++++
Rationale and extra context

Should also suggest the use of let else

Other cases

No response

Rust Version
rustc 1.78.0-nightly (3246e7951 2024-02-19)
binary: rustc
commit-hash: 3246e79513cb89ddbfc0f21cb5a877e5b321dcc5
commit-date: 2024-02-19
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and inspect the E0005 diagnostic described in the issue, including rustc --explain E0005. Trace where refutable local-binding suggestions are generated and determine how the let-else suggestion should be emitted. Done means the example reports a let else suggestion with the shown replacement without losing existing diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.