Skip to content

Recover from accidental inclusion of if in let else #103791

Closed
@Rageking8

Description

@Rageking8

Given the following code: link

fn main() {
    let x = Some(123);
    if let Some(y) = x else {
        return;
    };
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: this `if` expression is missing a block after the condition
 --> src/main.rs:3:5
  |
3 |     if let Some(y) = x else {
  |     ^^
  |
help: add a block here
 --> src/main.rs:3:23
  |
3 |     if let Some(y) = x else {
  |                       ^

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

Ideally the output should suggest removing the additional if keyword in front of the let else. This mistake can occur due to muscle memory as if, if let, let else and in the future let chains are somewhat similar syntactically. Thanks.

@rustbot label +D-confusing +F-let-else

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-confusingDiagnostics: Confusing error or lint that should be reworked.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-let_elseIssues related to let-else statements (RFC 3137)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