Skip to content

Parse error on let followed by a block should suggest likely alternatives if-let or let-else #107806

@pnkfelix

Description

@pnkfelix

Code

fn main() {
    let Some(msg) = opt_msg() {
        dbg!(msg);
    }
}

fn opt_msg() -> Option<&'static str> {
    Some("hello world")
}

Current output

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
 --> src/main.rs:2:31
  |
2 |     let Some(msg) = opt_msg() {
  |                               ^ expected one of `.`, `;`, `?`, `else`, or an operator

error: could not compile `playground` due to previous error

Desired output

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
 --> src/main.rs:2:31
  |
2 |     let Some(msg) = opt_msg() {
  |                               ^ expected one of `.`, `;`, `?`, `else`, or an operator

note: if you intended to write an `if let`, put an `if` in front of the `let`, like so:
  |
2 |     if let Some(msg) = opt_msg() {
  |

note: if you intended to write a `let else`, add an `else` and its block after the scrutinee, like so:

  |
2 |     let Some(msg) = opt_msg() else { ... } {
  |

error: could not compile `playground` due to previous error

Rationale and extra context

Its pretty natural at times to try to flip code from a let to an if let, but the resulting compiler parsing error is not as helpful as it could be.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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