-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 adding a =>
after match guard
#94932
Closed
TaKO8Ki
wants to merge
2
commits into
rust-lang:master
from
TaKO8Ki:suggest-adding-fat-arrow-after-match-guard
+95
−13
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2480,7 +2480,7 @@ impl<'a> Parser<'a> { | |
)?; | ||
let guard = if this.eat_keyword(kw::If) { | ||
let if_span = this.prev_token.span; | ||
let cond = this.parse_expr()?; | ||
let cond = this.parse_match_guard_cond()?; | ||
let (has_let_expr, does_not_have_bin_op) = check_let_expr(&cond); | ||
if has_let_expr { | ||
if does_not_have_bin_op { | ||
|
@@ -2496,6 +2496,15 @@ impl<'a> Parser<'a> { | |
}; | ||
let arrow_span = this.token.span; | ||
if let Err(mut err) = this.expect(&token::FatArrow) { | ||
if guard.is_some() && this.token.kind == token::OpenDelim(token::Brace) { | ||
err.span_suggestion_verbose( | ||
this.token.span.shrink_to_lo(), | ||
"try adding a fat arrow here", | ||
"=> ".to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
err.emit(); | ||
} | ||
// We might have a `=>` -> `=` or `->` typo (issue #89396). | ||
if TokenKind::FatArrow | ||
.similar_tokens() | ||
|
@@ -2617,6 +2626,26 @@ impl<'a> Parser<'a> { | |
} | ||
} | ||
|
||
fn parse_match_guard_cond(&mut self) -> PResult<'a, P<Expr>> { | ||
let snapshot = self.clone(); | ||
let mut cond = self.parse_expr()?; | ||
let (is_lhs_struct, is_rhs_struct) = match cond.kind { | ||
ExprKind::Binary(_, ref lhs, ref rhs) => { | ||
(matches!(lhs.kind, ExprKind::Struct(..)), matches!(rhs.kind, ExprKind::Struct(..))) | ||
} | ||
_ => (false, false), | ||
}; | ||
Comment on lines
+2632
to
+2637
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. As you recognize here, we might want to also detect when a struct literal was parsed that instead was supposed to be an ident and the match arm body, but that is not as critical, I think. |
||
if !is_lhs_struct | ||
&& (!is_rhs_struct | ||
|| is_rhs_struct && self.token.kind != token::OpenDelim(token::Brace)) | ||
&& self.token.kind != token::FatArrow | ||
{ | ||
*self = snapshot; | ||
cond = self.parse_cond_expr()?; | ||
} | ||
Ok(cond) | ||
} | ||
|
||
fn is_do_catch_block(&self) -> bool { | ||
self.token.is_keyword(kw::Do) | ||
&& self.is_keyword_ahead(1, &[kw::Catch]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[derive(PartialEq)] | ||
struct Foo { | ||
x: isize, | ||
} | ||
|
||
fn foo() { | ||
match () { | ||
() if f == Foo { x: 42 } {} | ||
//~^ ERROR expected one of `.`, `=>`, `?`, or an operator, found `{` | ||
_ => {} | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = 1; | ||
let y = 2; | ||
let value = 3; | ||
|
||
match value { | ||
Some(x) if x == y { | ||
//~^ ERROR expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{` | ||
x | ||
}, | ||
_ => { | ||
y | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/ui/parser/missing-fat-arrow-after-match-guard.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error: expected one of `.`, `=>`, `?`, or an operator, found `{` | ||
--> $DIR/missing-fat-arrow-after-match-guard.rs:8:34 | ||
| | ||
LL | () if f == Foo { x: 42 } {} | ||
| ^ expected one of `.`, `=>`, `?`, or an operator | ||
| | ||
help: try adding a fat arrow here | ||
| | ||
LL | () if f == Foo { x: 42 } => {} | ||
| ++ | ||
|
||
error: expected one of `!`, `.`, `::`, `=>`, `?`, or an operator, found `{` | ||
--> $DIR/missing-fat-arrow-after-match-guard.rs:20:27 | ||
| | ||
LL | Some(x) if x == y { | ||
| ^ expected one of `!`, `.`, `::`, `=>`, `?`, or an operator | ||
| | ||
help: try adding a fat arrow here | ||
| | ||
LL | Some(x) if x == y => { | ||
| ++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only issue I'm seeing here is that this pessimises the parsing of every match arm. In this case I think it is reasonable to instead catch the problem in line 2499 and only in the error arm try to conditionally parse the match arm body. Probably also do that for the substitution cases handled right below.