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

Increase accuracy of if condition misparse suggestion #133051

Merged
merged 4 commits into from
Nov 17, 2024

Conversation

estebank
Copy link
Contributor

Fix #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! {} {
   |            ++

@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2024

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2024
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! These suggestions are best-effort so that looks good to me. I have one uncertainty (heh) regarding how certain the "try using xxx" diagnostics sounds, but otherwise LGTM. The wording discussions are not blocking, if you prefer the current wording that's fine with me too, then just r=me.

{
// These are more likely to have been meant as a block body.
e.multipart_suggestion(
"try placing this code inside a block",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion [UNCERTAINTY 1/2]: "try placing this code inside a block" -> "maybe try placing this code inside a block"? My first reaction when reading this immediately is that it feels like it sounds too certain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "try..." wording is what we used to use back in the pre-2018 era, and had slowly tried to move away from it. Looking at this again, what do you think of "you might have meant to write this statement/expression as part of a new block" or similar?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me yeah

(token::OpenDelim(Delimiter::Brace), _) => {}
(_, _) => {
e.multipart_suggestion(
"try placing this code inside a block",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion [UNCERTAINTY 2/2]: ditto

@jieyouxu
Copy link
Member

r? jieyouxu

@rustbot rustbot assigned jieyouxu and unassigned Nadrieril Nov 15, 2024
@jieyouxu
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 15, 2024
// Speculative; has been misleading in the past (#46836).
Applicability::MaybeIncorrect,
);
match (&self.token.kind, &stmt.kind) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's 70 new lines of code, more than doubling the size of a nontrivial function. Maybe split parts out into appropriately named function(s)?

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you can r=me after pulling out the suggestion into a helper (or a few helpers) as per oli's suggestion.

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! {} {
   |            ++
```
@estebank
Copy link
Contributor Author

@bors r=jieyouxu

@bors
Copy link
Contributor

bors commented Nov 16, 2024

📌 Commit 6913194 has been approved by jieyouxu

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Nov 16, 2024

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 16, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 17, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#133029 (ABI checks: add support for some tier3 arches, warn on others.)
 - rust-lang#133051 (Increase accuracy of `if` condition misparse suggestion)
 - rust-lang#133060 (Trim whitespace in RemoveLet primary span)
 - rust-lang#133093 (Let chains tests)
 - rust-lang#133116 (stabilize const_ptr_is_null)
 - rust-lang#133126 (alloc: fix `String`'s doc)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2f62fd3 into rust-lang:master Nov 17, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Nov 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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