-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
fn a() { | ||
let Some(x) = foo() { //~ ERROR expected one of | ||
//~^ HELP you might have meant to use `if let` | ||
let y = x; | ||
} | ||
} | ||
fn b() { | ||
let Some(x) = foo() { //~ ERROR expected one of | ||
//~^ HELP you might have meant to use `let else` | ||
return; | ||
} | ||
} | ||
fn c() { | ||
let Some(x) = foo() { //~ ERROR expected one of | ||
//~^ HELP you might have meant to use `if let` | ||
//~| HELP alternatively, you might have meant to use `let else` | ||
// The parser check happens pre-macro-expansion, so we don't know for sure. | ||
println!("{x}"); | ||
} | ||
} | ||
fn foo() -> Option<i32> { | ||
Some(42) | ||
} | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` | ||
--> $DIR/missing-if-let-or-let-else.rs:2: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() { | ||
| ++ | ||
|
||
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` | ||
--> $DIR/missing-if-let-or-let-else.rs:8:25 | ||
| | ||
LL | let Some(x) = foo() { | ||
| ^ expected one of `.`, `;`, `?`, `else`, or an operator | ||
| | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. if we suggest add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
};
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The last example is valid, but unless Keep in mind that a person is still in the loop. If they didn't mean let-else, then it would give them enough information to realize that the code they wrote wasn't if-let. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, make sense. |
||
| ++++ | ||
|
||
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 { | ||
| ++++ | ||
|
||
error: aborting due to 3 previous errors | ||
|
Uh oh!
There was an error while loading. Please reload this page.