Skip to content

Commit 8d189ed

Browse files
committed
Suggest calling method when first argument is self
1 parent a916ac2 commit 8d189ed

File tree

6 files changed

+88
-7
lines changed

6 files changed

+88
-7
lines changed

src/doc/rustc-guide

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
759759
// These items live in both the type and value namespaces.
760760
ItemKind::Struct(ref vdata, _) => {
761761
// Define a name in the type namespace.
762-
let def_id = self.r.definitions.local_def_id(item.id);
763-
let res = Res::Def(DefKind::Struct, def_id);
762+
let item_def_id = self.r.definitions.local_def_id(item.id);
763+
let res = Res::Def(DefKind::Struct, item_def_id);
764764
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
765765

766766
// Record field names for error reporting.
@@ -798,12 +798,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
798798
}
799799

800800
ItemKind::Union(ref vdata, _) => {
801-
let def_id = self.r.definitions.local_def_id(item.id);
802-
let res = Res::Def(DefKind::Union, def_id);
801+
let item_def_id = self.r.definitions.local_def_id(item.id);
802+
let res = Res::Def(DefKind::Union, item_def_id);
803803
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
804804

805805
// Record field names for error reporting.
806-
self.insert_field_names_local(def_id, vdata);
806+
self.insert_field_names_local(item_def_id, vdata);
807807
}
808808

809809
ItemKind::Trait(..) => {

src/librustc_resolve/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ struct DiagnosticMetadata {
345345
/// The current self item if inside an ADT (used for better errors).
346346
current_self_item: Option<NodeId>,
347347

348-
/// The current enclosing funciton (used for better errors).
348+
/// The current enclosing function (used for better errors).
349349
current_function: Option<Span>,
350350

351351
/// A list of labels as of yet unused. Labels will be removed from this map when

src/librustc_resolve/late/diagnostics.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,37 @@ impl<'a> LateResolutionVisitor<'a, '_> {
259259
}
260260
return (err, candidates);
261261
}
262+
263+
// Check if the first argument is `self` and suggest calling a method.
264+
let mut has_self_arg = false;
265+
if let PathSource::Expr(parent) = source {
266+
match &parent.map(|p| &p.kind) {
267+
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
268+
let mut expr_kind = &args.first().unwrap().kind;
269+
loop {
270+
match expr_kind {
271+
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
272+
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
273+
break;
274+
},
275+
ExprKind::AddrOf(_, _, expr) => { expr_kind = &expr.kind; }
276+
_ => break,
277+
}
278+
}
279+
}
280+
_ => (),
281+
}
282+
};
283+
284+
if has_self_arg {
285+
err.span_suggestion(
286+
span,
287+
&"try calling method instead of passing `self` as parameter",
288+
format!("self.{}", path_str),
289+
Applicability::MachineApplicable,
290+
);
291+
return (err, candidates);
292+
}
262293
}
263294

264295
// Try Levenshtein algorithm.

src/test/ui/self/suggest-self-2.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Foo {}
2+
3+
impl Foo {
4+
fn foo(&self) {
5+
bar(self);
6+
//~^ ERROR cannot find function `bar` in this scope
7+
//~| HELP try calling method instead of passing `self` as parameter
8+
9+
10+
bar(&self);
11+
//~^ ERROR cannot find function `bar` in this scope
12+
//~| HELP try calling method instead of passing `self` as parameter
13+
14+
bar();
15+
//~^ ERROR cannot find function `bar` in this scope
16+
17+
self.bar();
18+
//~^ ERROR no method named `bar` found for type
19+
}
20+
}
21+
22+
fn main() {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0425]: cannot find function `bar` in this scope
2+
--> $DIR/suggest-self-2.rs:5:9
3+
|
4+
LL | bar(self);
5+
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
6+
7+
error[E0425]: cannot find function `bar` in this scope
8+
--> $DIR/suggest-self-2.rs:10:9
9+
|
10+
LL | bar(&self);
11+
| ^^^ help: try calling method instead of passing `self` as parameter: `self.bar`
12+
13+
error[E0425]: cannot find function `bar` in this scope
14+
--> $DIR/suggest-self-2.rs:14:9
15+
|
16+
LL | bar();
17+
| ^^^ not found in this scope
18+
19+
error[E0599]: no method named `bar` found for type `&Foo` in the current scope
20+
--> $DIR/suggest-self-2.rs:17:14
21+
|
22+
LL | self.bar();
23+
| ^^^ method not found in `&Foo`
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0425, E0599.
28+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)