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

Suggest if let when a failed if block could be parsed as one #44990

Closed
Tracked by #53667
estebank opened this issue Oct 3, 2017 · 4 comments · Fixed by #75931
Closed
Tracked by #53667

Suggest if let when a failed if block could be parsed as one #44990

estebank opened this issue Oct 3, 2017 · 4 comments · Fixed by #75931
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-let_chains `#![feature(let_chains)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@estebank
Copy link
Contributor

estebank commented Oct 3, 2017

When writing if Some(x) = foo {}, attempt to parse as if let Some(x) = foo {} for better diagnostic output and if successful provide a suggestion to add the let.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics labels Oct 3, 2017
@cramertj
Copy link
Member

cramertj commented Oct 3, 2017

Is if let the right suggestion here? Often the code would be fixed by just adding an extra =. This code is perfectly valid, for example:

fn main() {
    let x = 0;
    let foo = Some(0);
    if Some(x) == foo {}
}

@estebank
Copy link
Contributor Author

estebank commented Oct 3, 2017

I think it would if x wasn't in the scope. We can't know that during parsing, but the errors for

fn main() {
    let foo = Some(0);
    if Some(x) = foo {}
}

are

error[E0425]: cannot find value `x` in this scope
 --> src/main.rs:3:13
  |
3 |     if Some(x) = foo {}
  |             ^ not found in this scope

error[E0308]: mismatched types
 --> src/main.rs:3:8
  |
3 |     if Some(x) = foo {}
  |        ^^^^^^^^^^^^^
  |        |
  |        help: did you mean to compare equality?: `Some(x) == foo`
  |        expected bool, found ()
  |
  = note: expected type `bool`
             found type `()`

Which tells me we could catch not found in this scope errors that occur on if conditions and try them as if let. If it succeeds to parse the rest of the block, then emit the original error with an extra suggestion.

For the case where let x = 0 is in the code, the output is only the mismatched types error.

@estebank
Copy link
Contributor Author

cc @Centril, given the recent it let desugaring work.

@Centril
Copy link
Contributor

Centril commented May 23, 2019

@estebank Added to the tracking issue for list of potential issues to work on; tho since it is preexisting, not as a blocker but a nice-to-have.

@Centril Centril added the F-let_chains `#![feature(let_chains)]` label Oct 10, 2019
@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 22, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 1, 2020
Suggest `if let x = y` when encountering `if x = y`

Detect potential cases where `if let` was meant but `let` was left out.

Fix rust-lang#44990.
@bors bors closed this as completed in 07112ca Sep 1, 2020
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-let_chains `#![feature(let_chains)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants