Skip to content

Commit

Permalink
Extend suggestion span to whole method call
Browse files Browse the repository at this point in the history
  • Loading branch information
VirrageS committed Dec 22, 2019
1 parent e047368 commit 9273f59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
26 changes: 15 additions & 11 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,26 @@ 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 => {
let mut expr_kind = &args[0].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;
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;
},
Expand All @@ -283,7 +287,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) {
Expand All @@ -292,7 +296,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,
Expand Down
12 changes: 9 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,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
Expand Down

0 comments on commit 9273f59

Please sign in to comment.