Skip to content

Detect incorrect chaining of if and if let conditions and recover gracefully #103381

Closed
@estebank

Description

@estebank

Given

fn foo(b: bool, x: Option<u32>) {
    if b && if let Some(x) = x {}
}
fn main() {
    if true && if true { true }
}

the current output is

error: expected `{`, found `}`
 --> src/main.rs:3:1
  |
3 | }
  | ^ expected `{`
  |
note: the `if` expression is missing a block after this condition
 --> src/main.rs:2:8
  |
2 |     if b && if let Some(x) = x {}
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected `{`, found `}`
 --> src/main.rs:6:1
  |
6 | }
  | ^ expected `{`
  |
note: the `if` expression is missing a block after this condition
 --> src/main.rs:5:8
  |
5 |     if true && if true { true }
  |        ^^^^^^^^^^^^^^^^^^^^^^^^

We should detect that what was intended was

fn foo(b: bool, x: Option<u32>) {
    if b && let Some(x) = x {}
}
fn main() {
    if true && true { true }
}

#53667 (comment)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.F-let_chains`#![feature(let_chains)]`P-lowLow priorityT-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