Skip to content

Commit cf19131

Browse files
committed
Try to recover from a => -> = or -> typo in a match arm
1 parent edebf77 commit cf19131

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2322,7 +2322,21 @@ 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 let token::Eq | token::RArrow = this.token.kind {
2328+
err.span_suggestion(
2329+
this.token.span,
2330+
"try using a fat arrow here",
2331+
"=>".to_string(),
2332+
Applicability::MaybeIncorrect,
2333+
);
2334+
err.emit();
2335+
this.bump();
2336+
} else {
2337+
return Err(err);
2338+
}
2339+
}
23262340
let arm_start_span = this.token.span;
23272341

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

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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 20 additions & 0 deletions
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)