Skip to content

Commit

Permalink
Provide suggestion to users with naughty elves
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiretza committed Apr 27, 2024
1 parent 61a1dbd commit 7a74f33
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
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

0 comments on commit 7a74f33

Please sign in to comment.