Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pnkfelix opened this issue Feb 8, 2023 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Feb 8, 2023

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

@pnkfelix pnkfelix added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 8, 2023
@obeis obeis removed their assignment Jan 14, 2024
@fmease fmease added A-parser Area: The parsing of Rust source code to an AST D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. labels Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants