-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Detect missing if let
or let-else
#145582
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
base: master
Are you sure you want to change the base?
Conversation
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
I'd like to have a review on it |
This comment has been minimized.
This comment has been minimized.
During `let` binding parse error and encountering a block, detect if there is a likely missing `if` or `else`: ``` error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` --> $DIR/missing-if-let-or-let-else.rs:14:25 | LL | let Some(x) = foo() { | ^ expected one of `.`, `;`, `?`, `else`, or an operator | help: you might have meant to use `if let` | LL | if let Some(x) = foo() { | ++ help: alternatively, you might have meant to use `let else` | LL | let Some(x) = foo() else { | ++++ ```
If we wanted to improve the handling of these, then we would have to actually add a new error recovery AST node, with all the information that an |
| | ||
help: you might have meant to use `let else` | ||
| | ||
LL | let Some(x) = foo() else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we suggest add else
here, a following error missing ;
will comes after applying the fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the correct code maybe:
let Some(x) = foo() else {
return;
}; // need `;` here
i'm not sure whether we should also suggest the ;
at the same time, maybe goes too far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emm, this code is also valid 😀
fn b() {
if let Some(x) = foo() {
return;
};
}
} | ||
true | ||
}); | ||
// Collect all bindings in pattern and see if they appear in the block. Likely meant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe we should move the definition of IdentFinder
to here, since it's only used in this function. Otherwise IdentFinder
is a too general visitor name.
During
let
binding parse error and encountering a block, detect if there is a likely missingif
orelse
:Fix #107806.