Skip to content

Commit

Permalink
Rollup merge of rust-lang#100253 - obeis:issue-100197, r=cjgillot
Browse files Browse the repository at this point in the history
Recover from mutable variable declaration where `mut` is placed before `let`

Closes rust-lang#100197
  • Loading branch information
matthiaskrgr authored Aug 14, 2022
2 parents c50c45f + 59e4063 commit 431f931
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ impl<'a> Parser<'a> {
return Ok(Some(stmt.into_inner()));
}

if self.token.is_keyword(kw::Mut) && self.is_keyword_ahead(1, &[kw::Let]) {
self.bump();
let mut_let_span = lo.to(self.token.span);
self.struct_span_err(mut_let_span, "invalid variable declaration")
.span_suggestion(
mut_let_span,
"switch the order of `mut` and `let`",
"let mut",
Applicability::MaybeIncorrect,
)
.emit();
}

Ok(Some(if self.token.is_keyword(kw::Let) {
self.parse_local_mk(lo, attrs, capture_semi, force_collect)?
} else if self.is_kw_followed_by_ident(kw::Mut) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/parser/issue-100197-mut-let.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix

fn main() {
let mut _x = 123;
//~^ ERROR invalid variable declaration
}
6 changes: 6 additions & 0 deletions src/test/ui/parser/issue-100197-mut-let.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix

fn main() {
mut let _x = 123;
//~^ ERROR invalid variable declaration
}
8 changes: 8 additions & 0 deletions src/test/ui/parser/issue-100197-mut-let.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: invalid variable declaration
--> $DIR/issue-100197-mut-let.rs:4:5
|
LL | mut let _x = 123;
| ^^^^^^^ help: switch the order of `mut` and `let`: `let mut`

error: aborting due to previous error

0 comments on commit 431f931

Please sign in to comment.