Extend suggestion span to whole method call

This commit is contained in:
Janusz Marcinkiewicz 2019-12-22 22:48:45 +01:00
parent 091853946b
commit 7353afdfd9
2 changed files with 24 additions and 14 deletions

View file

@ -261,8 +261,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;
let mut has_self_arg = None;
if let PathSource::Expr(parent) = source {
match &parent.map(|p| &p.kind) {
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
@ -270,13 +269,18 @@ impl<'a> LateResolutionVisitor<'a, '_> {
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;
if args.len() > 1 {
args_span = Some(Span::new(
args[1].span.lo(),
args.last().unwrap().span.hi(),
parent.unwrap().span.ctxt(),
));
if arg_name.segments[0].ident.name == kw::SelfLower {
let call_span = parent.unwrap().span;
let args_span = if args.len() > 1 {
Some(Span::new(
args[1].span.lo(),
args.last().unwrap().span.hi(),
call_span.ctxt(),
))
} else {
None
};
has_self_arg = Some((call_span, args_span));
}
break;
},
@ -289,7 +293,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
}
};
if has_self_arg {
if let Some((call_span, args_span)) = 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) {
@ -298,7 +302,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
}
err.span_suggestion(
span,
call_span,
&format!("try calling `{}` as a method", ident),
format!("self.{}({})", path_str, args_snippet),
Applicability::MachineApplicable,

View file

@ -2,19 +2,25 @@ 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(102)`
| ^^^-------------
| |
| 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(102, &"str")`
| ^^^------------------------
| |
| 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