Skip to content

Commit 0918539

Browse files
committed
Add arguments to suggestion method call
1 parent 8e5b2c8 commit 0918539

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/librustc_resolve/late/diagnostics.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
262262

263263
// Check if the first argument is `self` and suggest calling a method.
264264
let mut has_self_arg = false;
265+
let mut args_span = None;
265266
if let PathSource::Expr(parent) = source {
266267
match &parent.map(|p| &p.kind) {
267268
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
@@ -270,6 +271,13 @@ impl<'a> LateResolutionVisitor<'a, '_> {
270271
match expr_kind {
271272
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
272273
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
274+
if args.len() > 1 {
275+
args_span = Some(Span::new(
276+
args[1].span.lo(),
277+
args.last().unwrap().span.hi(),
278+
parent.unwrap().span.ctxt(),
279+
));
280+
}
273281
break;
274282
},
275283
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
@@ -282,10 +290,17 @@ impl<'a> LateResolutionVisitor<'a, '_> {
282290
};
283291

284292
if has_self_arg {
293+
let mut args_snippet: String = String::from("");
294+
if let Some(args_span) = args_span {
295+
if let Ok(snippet) = self.r.session.source_map().span_to_snippet(args_span) {
296+
args_snippet = snippet;
297+
}
298+
}
299+
285300
err.span_suggestion(
286301
span,
287302
&format!("try calling `{}` as a method", ident),
288-
format!("self.{}", path_str),
303+
format!("self.{}({})", path_str, args_snippet),
289304
Applicability::MachineApplicable,
290305
);
291306
return (err, candidates);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error[E0425]: cannot find function `bar` in this scope
22
--> $DIR/suggest-self-2.rs:5:9
33
|
44
LL | bar(self);
5-
| ^^^ help: try calling `bar` as a method: `self.bar`
5+
| ^^^ help: try calling `bar` as a method: `self.bar()`
66

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

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

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

0 commit comments

Comments
 (0)