Suggest calling method when first argument is self

This commit is contained in:
Janusz Marcinkiewicz 2019-11-30 18:59:51 +01:00
parent a916ac22b9
commit 8d189ed2f1
6 changed files with 88 additions and 7 deletions

@ -1 +1 @@
Subproject commit 7c56708aab7986ca390221e8e8902f7de7f9b076
Subproject commit 934380b7cfceaaa4e1b9bb0de4a372f32725520b

View File

@ -759,8 +759,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
// These items live in both the type and value namespaces.
ItemKind::Struct(ref vdata, _) => {
// Define a name in the type namespace.
let def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Struct, def_id);
let item_def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Struct, item_def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
// Record field names for error reporting.
@ -798,12 +798,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
ItemKind::Union(ref vdata, _) => {
let def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Union, def_id);
let item_def_id = self.r.definitions.local_def_id(item.id);
let res = Res::Def(DefKind::Union, item_def_id);
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
// Record field names for error reporting.
self.insert_field_names_local(def_id, vdata);
self.insert_field_names_local(item_def_id, vdata);
}
ItemKind::Trait(..) => {

View File

@ -345,7 +345,7 @@ struct DiagnosticMetadata {
/// The current self item if inside an ADT (used for better errors).
current_self_item: Option<NodeId>,
/// The current enclosing funciton (used for better errors).
/// The current enclosing function (used for better errors).
current_function: Option<Span>,
/// A list of labels as of yet unused. Labels will be removed from this map when

View File

@ -259,6 +259,37 @@ impl<'a> LateResolutionVisitor<'a, '_> {
}
return (err, candidates);
}
// Check if the first argument is `self` and suggest calling a method.
let mut has_self_arg = false;
if let PathSource::Expr(parent) = source {
match &parent.map(|p| &p.kind) {
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
let mut expr_kind = &args.first().unwrap().kind;
loop {
match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
break;
},
ExprKind::AddrOf(_, _, expr) => { expr_kind = &expr.kind; }
_ => break,
}
}
}
_ => (),
}
};
if has_self_arg {
err.span_suggestion(
span,
&"try calling method instead of passing `self` as parameter",
format!("self.{}", path_str),
Applicability::MachineApplicable,
);
return (err, candidates);
}
}
// Try Levenshtein algorithm.

View File

@ -0,0 +1,22 @@
struct Foo {}
impl Foo {
fn foo(&self) {
bar(self);
//~^ ERROR cannot find function `bar` in this scope
//~| HELP try calling method instead of passing `self` as parameter
bar(&self);
//~^ ERROR cannot find function `bar` in this scope
//~| HELP try calling method instead of passing `self` as parameter
bar();
//~^ ERROR cannot find function `bar` in this scope
self.bar();
//~^ ERROR no method named `bar` found for type
}
}
fn main() {}

View File

@ -0,0 +1,28 @@
error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:5:9
|
LL | bar(self);
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:10:9
|
LL | bar(&self);
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:14:9
|
LL | bar();
| ^^^ not found in this scope
error[E0599]: no method named `bar` found for type `&Foo` in the current scope
--> $DIR/suggest-self-2.rs:17:14
|
LL | self.bar();
| ^^^ method not found in `&Foo`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0425, E0599.
For more information about an error, try `rustc --explain E0425`.