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

Misparse in for expressions always suggest wrapping with a body, when it might be something else #132656

Closed
estebank opened this issue Nov 5, 2024 · 0 comments · Fixed by #133051
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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Nov 5, 2024

Code

fn main() {
    for _ in [1, 2, 3].iter()map(|x| x) {}
}

Current output

error: expected `{`, found `map`
 --> src/main.rs:2:30
  |
2 |     for _ in [1, 2, 3].iter()map(|x| x) {}
  |                              ^^^ expected `{`
  |
help: try placing this code inside a block
  |
2 |     for _ in [1, 2, 3].iter(){ map(|x| x) } {}
  |                              +            +

Desired output

error: expected `{`, found `map`
 --> src/main.rs:2:30
  |
2 |     for _ in [1, 2, 3].iter()map(|x| x) {}
  |                              ^^^ expected `{`
  |
help: you might have meant to call a method
  |
2 |     for _ in [1, 2, 3].iter().map(|x| x) {}
  |                              +

Rationale and extra context

No response

Other cases

No response

Rust Version

1.82

Anything else?

No response

@estebank estebank added 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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 5, 2024
@chenyukang chenyukang self-assigned this Nov 9, 2024
@jieyouxu jieyouxu assigned estebank and unassigned chenyukang Nov 15, 2024
@bors bors closed this as completed in 2f62fd3 Nov 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 17, 2024
Rollup merge of rust-lang#133051 - estebank:cond-misparse, r=jieyouxu

Increase accuracy of `if` condition misparse suggestion

Fix rust-lang#132656.

Look at the expression that was parsed when trying to recover from a bad `if` condition to determine what was likely intended by the user beyond "maybe this was meant to be an `else` body".

```
error: expected `{`, found `map`
  --> $DIR/missing-dot-on-if-condition-expression-fixable.rs:4:30
   |
LL |     for _ in [1, 2, 3].iter()map(|x| x) {}
   |                              ^^^ expected `{`
   |
help: you might have meant to write a method call
   |
LL |     for _ in [1, 2, 3].iter().map(|x| x) {}
   |                              +
```

If a macro statement has been parsed after `else`, suggest a missing `if`:

```
error: expected `{`, found `falsy`
  --> $DIR/else-no-if.rs:47:12
   |
LL |     } else falsy! {} {
   |       ---- ^^^^^
   |       |
   |       expected an `if` or a block after this `else`
   |
help: add an `if` if this is the condition of a chained `else if` statement
   |
LL |     } else if falsy! {} {
   |            ++
```
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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

2 participants