Skip to content

Detect extra space in keyword for better hint #113309

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

Closed
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,22 @@ impl<'a> Parser<'a> {
}
}

if let TokenKind::Ident(prev, _) = &self.prev_token.kind
&& let TokenKind::Ident(cur, _) = &self.token.kind
{
let concat = Symbol::intern(&format!("{}{}", prev, cur));
let ident = Ident::new(concat, DUMMY_SP);
if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() {
let span = self.prev_token.span.to(self.token.span);
err.span_suggestion_verbose(
span,
format!("consider removing the space to spell keyword `{}`", concat),
concat,
Applicability::MachineApplicable,
);
}
}

// `pub` may be used for an item or `pub(crate)`
if self.prev_token.is_ident_named(sym::public)
&& (self.token.can_begin_item()
Expand Down
Binary file modified tests/ui/parser/utf16-be-without-bom.stderr
Binary file not shown.
Binary file modified tests/ui/parser/utf16-le-without-bom.stderr
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/ui/suggestions/issue-89640.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
le t x: i32 = 3; //~ ERROR expected one of
}
13 changes: 13 additions & 0 deletions tests/ui/suggestions/issue-89640.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `t`
--> $DIR/issue-89640.rs:2:8
|
LL | le t x: i32 = 3;
| ^ expected one of 8 possible tokens
|
help: consider removing the space to spell keyword `let`
|
LL | let x: i32 = 3;
| ~~~

error: aborting due to previous error