Add arguments to suggestion method call

This commit is contained in:
Janusz Marcinkiewicz 2019-12-21 19:13:12 +01:00
parent 8e5b2c80d3
commit 091853946b
2 changed files with 19 additions and 4 deletions

View file

@ -262,6 +262,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 => {
@ -270,6 +271,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,
@ -282,10 +290,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);

View file

@ -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