From ea2df3a7982e4e011692ab0db89a9b465afda4af Mon Sep 17 00:00:00 2001 From: Obei Sideg Date: Mon, 8 Aug 2022 03:19:37 +0300 Subject: Recover from mutable variable declaration where `mut` is placed before `let` --- compiler/rustc_parse/src/parser/stmt.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 51bd9d2d386..697d9cecfe6 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -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) { -- cgit 1.4.1-3-g733a5