Skip to content

Commit

Permalink
Add arguments to suggestion method call
Browse files Browse the repository at this point in the history
  • Loading branch information
VirrageS committed Dec 21, 2019
1 parent 4b6305c commit e047368
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {

// Check if the first argument is `self` and suggest calling a method.
let mut has_self_arg = false;
let mut args_span = None;
if let PathSource::Expr(parent) = source {
match &parent.map(|p| &p.kind) {
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
Expand All @@ -264,6 +265,13 @@ impl<'a> LateResolutionVisitor<'a, '_> {
match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
if args.len() > 1 {
args_span = Some(Span::new(
args[1].span.lo(),
args.last().unwrap().span.hi(),
parent.unwrap().span.ctxt(),
));
}
break;
},
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
Expand All @@ -276,10 +284,17 @@ impl<'a> LateResolutionVisitor<'a, '_> {
};

if has_self_arg {
let mut args_snippet: String = String::from("");
if let Some(args_span) = args_span {
if let Ok(snippet) = self.r.session.source_map().span_to_snippet(args_span) {
args_snippet = snippet;
}
}

err.span_suggestion(
span,
&format!("try calling `{}` as a method", ident),
format!("self.{}", path_str),
format!("self.{}({})", path_str, args_snippet),
Applicability::MachineApplicable,
);
return (err, candidates);
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/self/suggest-self-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:5:9
|
LL | bar(self);
| ^^^ help: try calling `bar` as a method: `self.bar`
| ^^^ help: try calling `bar` as a method: `self.bar()`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:9:9
|
LL | bar(&&self, 102);
| ^^^ help: try calling `bar` as a method: `self.bar`
| ^^^ help: try calling `bar` as a method: `self.bar(102)`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:13:9
|
LL | bar(&mut self, 102, &"str");
| ^^^ help: try calling `bar` as a method: `self.bar`
| ^^^ help: try calling `bar` as a method: `self.bar(102, &"str")`

error[E0425]: cannot find function `bar` in this scope
--> $DIR/suggest-self-2.rs:17:9
Expand Down

0 comments on commit e047368

Please sign in to comment.