Skip to content

Commit 13b8b6e

Browse files
authored
Rollup merge of #100253 - obeis:issue-100197, r=cjgillot
Recover from mutable variable declaration where `mut` is placed before `let` Closes #100197
2 parents b8b3ead + 59e4063 commit 13b8b6e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

compiler/rustc_parse/src/parser/stmt.rs

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ impl<'a> Parser<'a> {
5555
return Ok(Some(stmt.into_inner()));
5656
}
5757

58+
if self.token.is_keyword(kw::Mut) && self.is_keyword_ahead(1, &[kw::Let]) {
59+
self.bump();
60+
let mut_let_span = lo.to(self.token.span);
61+
self.struct_span_err(mut_let_span, "invalid variable declaration")
62+
.span_suggestion(
63+
mut_let_span,
64+
"switch the order of `mut` and `let`",
65+
"let mut",
66+
Applicability::MaybeIncorrect,
67+
)
68+
.emit();
69+
}
70+
5871
Ok(Some(if self.token.is_keyword(kw::Let) {
5972
self.parse_local_mk(lo, attrs, capture_semi, force_collect)?
6073
} else if self.is_kw_followed_by_ident(kw::Mut) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let mut _x = 123;
5+
//~^ ERROR invalid variable declaration
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
mut let _x = 123;
5+
//~^ ERROR invalid variable declaration
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: invalid variable declaration
2+
--> $DIR/issue-100197-mut-let.rs:4:5
3+
|
4+
LL | mut let _x = 123;
5+
| ^^^^^^^ help: switch the order of `mut` and `let`: `let mut`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)