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

Provide suggestion to users with naughty elves #124446

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 21 additions & 8 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use rustc_ast::tokenstream::AttrTokenTree;
use rustc_ast::util::parser::AssocOp;
use rustc_ast::{
AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Param, Pat,
PatKind, Path, PathSegment, QSelf, Ty, TyKind,
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Mutability,
Param, Pat, PatKind, Path, PathSegment, QSelf, Ty, TyKind,
};
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
Expand Down Expand Up @@ -2204,12 +2204,25 @@ impl<'a> Parser<'a> {
let ident = self.parse_ident().unwrap();
let span = pat.span.with_hi(ident.span.hi());

err.span_suggestion(
span,
"declare the type after the parameter binding",
"<identifier>: <type>",
Applicability::HasPlaceholders,
);
if let PatKind::Ref(pat, Mutability::Not) = &pat.kind
&& let PatKind::Ident(BindingMode::NONE, smut, None) = pat.kind
&& smut.as_str() == "smut"
&& ident.as_str() == "elf"
{
err.span_suggestion(
span,
"consider making this elf less naughty",
"&mut self",
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
span,
"declare the type after the parameter binding",
"<identifier>: <type>",
Applicability::HasPlaceholders,
);
}
return Some(ident);
} else if require_name
&& (self.token == token::Comma
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/parser/naughty-elves.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
struct Foo;

impl Foo {
fn bar(&smut elf) {}
//~^ ERROR expected one of `:`, `@`, or `|`
//~| HELP consider making this elf less naughty
//~| SUGGESTION &mut self
}

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/parser/naughty-elves.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: expected one of `:`, `@`, or `|`, found `elf`
--> $DIR/naughty-elves.rs:4:18
|
LL | fn bar(&smut elf) {}
| ------^^^
| | |
| | expected one of `:`, `@`, or `|`
| help: consider making this elf less naughty: `&mut self`

error: aborting due to 1 previous error