Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest removing let if const let or let const is used #100115

Merged
merged 3 commits into from
Aug 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
@@ -1124,6 +1124,16 @@ impl<'a> Parser<'a> {
Applicability::MaybeIncorrect,
)
.emit();
} else if self.eat_keyword(kw::Let) {
let span = self.prev_token.span;
self.struct_span_err(const_span.to(span), "`const` and `let` are mutually exclusive")
.span_suggestion(
const_span.to(span),
"remove `let`",
"const",
Applicability::MaybeIncorrect,
)
.emit();
}
}

16 changes: 16 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
@@ -247,6 +247,22 @@ impl<'a> Parser<'a> {
/// Parses a local variable declaration.
fn parse_local(&mut self, attrs: AttrVec) -> PResult<'a, P<Local>> {
let lo = self.prev_token.span;

if self.token.is_keyword(kw::Const) && self.look_ahead(1, |t| t.is_ident()) {
self.struct_span_err(
lo.to(self.token.span),
"`const` and `let` are mutually exclusive",
)
.span_suggestion(
lo.to(self.token.span),
"remove `let`",
"const",
Applicability::MaybeIncorrect,
)
.emit();
self.bump();
}

let (pat, colon) = self.parse_pat_before_ty(None, RecoverComma::Yes, "`let` bindings")?;

let (err, ty) = if colon {
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn main() {
const _FOO: i32 = 123;
//~^ ERROR const` and `let` are mutually exclusive
const _BAR: i32 = 123;
//~^ ERROR `const` and `let` are mutually exclusive
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn main() {
const let _FOO: i32 = 123;
//~^ ERROR const` and `let` are mutually exclusive
let const _BAR: i32 = 123;
//~^ ERROR `const` and `let` are mutually exclusive
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/issue-99910-const-let-mutually-exclusive.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: `const` and `let` are mutually exclusive
--> $DIR/issue-99910-const-let-mutually-exclusive.rs:4:5
|
LL | const let _FOO: i32 = 123;
| ^^^^^^^^^ help: remove `let`: `const`

error: `const` and `let` are mutually exclusive
--> $DIR/issue-99910-const-let-mutually-exclusive.rs:6:5
|
LL | let const _BAR: i32 = 123;
| ^^^^^^^^^ help: remove `let`: `const`

error: aborting due to 2 previous errors