Diagnostics for let mut in item context

This commit is contained in:
Kornel 2024-11-17 21:43:55 +00:00
parent a8e75c53d0
commit 7765f23ea1
No known key found for this signature in database
4 changed files with 47 additions and 9 deletions

View File

@ -77,18 +77,35 @@ impl<'a> Parser<'a> {
if !self.eat(term) { if !self.eat(term) {
let token_str = super::token_descr(&self.token); let token_str = super::token_descr(&self.token);
if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) { if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
let is_let = self.token.is_keyword(kw::Let);
let is_let_mut = is_let && self.look_ahead(1, |t| t.is_keyword(kw::Mut));
let let_has_ident = is_let && !is_let_mut && self.is_kw_followed_by_ident(kw::Let);
let msg = format!("expected item, found {token_str}"); let msg = format!("expected item, found {token_str}");
let mut err = self.dcx().struct_span_err(self.token.span, msg); let mut err = self.dcx().struct_span_err(self.token.span, msg);
let span = self.token.span;
if self.is_kw_followed_by_ident(kw::Let) { let label = if is_let {
err.span_label( "`let` cannot be used for global variables"
span,
"consider using `const` or `static` instead of `let` for global variables",
);
} else { } else {
err.span_label(span, "expected item") "expected item"
.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
}; };
err.span_label(self.token.span, label);
if is_let {
if is_let_mut {
err.help("consider using `static` and a `Mutex` instead of `let mut`");
} else if let_has_ident {
err.span_suggestion_short(
self.token.span,
"consider using `static` or `const` instead of `let`",
"static",
Applicability::MaybeIncorrect,
);
} else {
err.help("consider using `static` or `const` instead of `let`");
}
}
err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
return Err(err); return Err(err);
} }
} }

View File

@ -2,7 +2,12 @@ error: expected item, found keyword `let`
--> $DIR/suggest-const-for-global-var.rs:1:1 --> $DIR/suggest-const-for-global-var.rs:1:1
| |
LL | let X: i32 = 12; LL | let X: i32 = 12;
| ^^^ consider using `const` or `static` instead of `let` for global variables | ^^^
| |
| `let` cannot be used for global variables
| help: consider using `static` or `const` instead of `let`
|
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 1 previous error error: aborting due to 1 previous error

View File

@ -0,0 +1,5 @@
let mut _data = vec![1,2,3];
//~^ ERROR expected item, found keyword `let`
fn main() {
}

View File

@ -0,0 +1,11 @@
error: expected item, found keyword `let`
--> $DIR/suggest-static-for-global-var-mut.rs:1:1
|
LL | let mut _data = vec![1,2,3];
| ^^^ `let` cannot be used for global variables
|
= help: consider using `static` and a `Mutex` instead of `let mut`
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
error: aborting due to 1 previous error