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

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

Closed
estebank opened this issue Oct 22, 2022 · 2 comments · Fixed by #103405
Closed

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

estebank opened this issue Oct 22, 2022 · 2 comments · Fixed by #103405
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. F-let_chains `#![feature(let_chains)]` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Oct 22, 2022

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)

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Oct 22, 2022
@chenyukang
Copy link
Member

@rustbot claim

@est31
Copy link
Member

est31 commented Oct 24, 2022

@rustbot label F-let_chains

@rustbot rustbot added the F-let_chains `#![feature(let_chains)]` label Oct 24, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 18, 2022
…f, r=compiler-errors

Detect incorrect chaining of if and if let conditions and recover

Fixes rust-lang#103381
@bors bors closed this as completed in 74c9a6c Nov 18, 2022
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. F-let_chains `#![feature(let_chains)]` P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants