Skip to content

Commit 90e96f9

Browse files
Rollup merge of #89487 - FabianWolff:issue-89396, r=petrochenkov
Try to recover from a `=>` -> `=` or `->` typo in a match arm Fixes #89396.
2 parents 25cc28e + 079c075 commit 90e96f9

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

compiler/rustc_ast/src/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl TokenKind {
295295
match *self {
296296
Comma => Some(vec![Dot, Lt, Semi]),
297297
Semi => Some(vec![Colon, Comma]),
298+
FatArrow => Some(vec![Eq, RArrow]),
298299
_ => None,
299300
}
300301
}

compiler/rustc_parse/src/parser/expr.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -2322,7 +2322,24 @@ impl<'a> Parser<'a> {
23222322
None
23232323
};
23242324
let arrow_span = this.token.span;
2325-
this.expect(&token::FatArrow)?;
2325+
if let Err(mut err) = this.expect(&token::FatArrow) {
2326+
// We might have a `=>` -> `=` or `->` typo (issue #89396).
2327+
if TokenKind::FatArrow
2328+
.similar_tokens()
2329+
.map_or(false, |similar_tokens| similar_tokens.contains(&this.token.kind))
2330+
{
2331+
err.span_suggestion(
2332+
this.token.span,
2333+
"try using a fat arrow here",
2334+
"=>".to_string(),
2335+
Applicability::MaybeIncorrect,
2336+
);
2337+
err.emit();
2338+
this.bump();
2339+
} else {
2340+
return Err(err);
2341+
}
2342+
}
23262343
let arm_start_span = this.token.span;
23272344

23282345
let expr = this.parse_expr_res(Restrictions::STMT_EXPR, None).map_err(|mut err| {

src/test/ui/parser/issue-89396.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for issue #89396: Try to recover from a
2+
// `=>` -> `=` or `->` typo in a match arm.
3+
4+
// run-rustfix
5+
6+
fn main() {
7+
let opt = Some(42);
8+
let _ = match opt {
9+
Some(_) => true,
10+
//~^ ERROR: expected one of
11+
//~| HELP: try using a fat arrow here
12+
None => false,
13+
//~^ ERROR: expected one of
14+
//~| HELP: try using a fat arrow here
15+
};
16+
}

src/test/ui/parser/issue-89396.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for issue #89396: Try to recover from a
2+
// `=>` -> `=` or `->` typo in a match arm.
3+
4+
// run-rustfix
5+
6+
fn main() {
7+
let opt = Some(42);
8+
let _ = match opt {
9+
Some(_) = true,
10+
//~^ ERROR: expected one of
11+
//~| HELP: try using a fat arrow here
12+
None -> false,
13+
//~^ ERROR: expected one of
14+
//~| HELP: try using a fat arrow here
15+
};
16+
}

src/test/ui/parser/issue-89396.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: expected one of `=>`, `if`, or `|`, found `=`
2+
--> $DIR/issue-89396.rs:9:17
3+
|
4+
LL | Some(_) = true,
5+
| ^
6+
| |
7+
| expected one of `=>`, `if`, or `|`
8+
| help: try using a fat arrow here: `=>`
9+
10+
error: expected one of `=>`, `@`, `if`, or `|`, found `->`
11+
--> $DIR/issue-89396.rs:12:14
12+
|
13+
LL | None -> false,
14+
| ^^
15+
| |
16+
| expected one of `=>`, `@`, `if`, or `|`
17+
| help: try using a fat arrow here: `=>`
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)