You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggest if let when a failed if block could be parsed as one #44990
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.
Reacted by oli, leonardo-m, Tarık Binar and Snir Broshi
I think it would if x wasn't in the scope. We can't know that during parsing, but the errors for
fnmain(){let foo = Some(0);ifSome(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.
Activity
cramertj commentedon Oct 3, 2017
Is
if letthe right suggestion here? Often the code would be fixed by just adding an extra=. This code is perfectly valid, for example:estebank commentedon Oct 3, 2017
I think it would if
xwasn't in the scope. We can't know that during parsing, but the errors forare
Which tells me we could catch
not found in this scopeerrors that occur on if conditions and try them asif 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 = 0is in the code, the output is only the mismatched types error.estebank commentedon May 22, 2019
cc @Centril, given the recent
it letdesugaring work.Centril commentedon 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.
Auto merge of rust-lang#75931 - estebank:suggest-if-let, r=petrochenkov