Skip to content

Commit

Permalink
suggest const or static for global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Aug 11, 2022
1 parent 1603a70 commit 98518c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ impl<'a> Parser<'a> {
if !self.maybe_consume_incorrect_semicolon(&items) {
let msg = &format!("expected item, found {token_str}");
let mut err = self.struct_span_err(self.token.span, msg);
err.span_label(self.token.span, "expected item");
let label = if self.is_kw_followed_by_ident(kw::Let) {
"consider using `const` or `static` instead of `let` for global variables"
} else {
"expected item"
};
err.span_label(self.token.span, label);
return Err(err);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/parser/suggest-const-for-global-var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let X: i32 = 12;
//~^ ERROR expected item, found keyword `let`

fn main() {
println!("{}", X);
}
8 changes: 8 additions & 0 deletions src/test/ui/parser/suggest-const-for-global-var.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected item, found keyword `let`
--> $DIR/suggest-const-for-global-var.rs:1:1
|
LL | let X: i32 = 12;
| ^^^ consider using `const` or `static` instead of `let` for global variables

error: aborting due to previous error

0 comments on commit 98518c2

Please sign in to comment.