Skip to content

Commit

Permalink
Rollup merge of #100115 - obeis:issue-99910, r=cjgillot
Browse files Browse the repository at this point in the history
Suggest removing `let` if `const let` or `let const` is used

Closes #99910
  • Loading branch information
Dylan-DPC committed Aug 14, 2022
2 parents 482a6ea + b3f32d1 commit 7473484
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,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();
}
}

Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
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

0 comments on commit 7473484

Please sign in to comment.