Skip to content

Commit e047368

Browse files
committed
Add arguments to suggestion method call
1 parent 4b6305c commit e047368

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/librustc_resolve/late/diagnostics.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
256256

257257
// Check if the first argument is `self` and suggest calling a method.
258258
let mut has_self_arg = false;
259+
let mut args_span = None;
259260
if let PathSource::Expr(parent) = source {
260261
match &parent.map(|p| &p.kind) {
261262
Some(ExprKind::Call(_, args)) if args.len() > 0 => {
@@ -264,6 +265,13 @@ impl<'a> LateResolutionVisitor<'a, '_> {
264265
match expr_kind {
265266
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
266267
has_self_arg = arg_name.segments[0].ident.name == kw::SelfLower;
268+
if args.len() > 1 {
269+
args_span = Some(Span::new(
270+
args[1].span.lo(),
271+
args.last().unwrap().span.hi(),
272+
parent.unwrap().span.ctxt(),
273+
));
274+
}
267275
break;
268276
},
269277
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
@@ -276,10 +284,17 @@ impl<'a> LateResolutionVisitor<'a, '_> {
276284
};
277285

278286
if has_self_arg {
287+
let mut args_snippet: String = String::from("");
288+
if let Some(args_span) = args_span {
289+
if let Ok(snippet) = self.r.session.source_map().span_to_snippet(args_span) {
290+
args_snippet = snippet;
291+
}
292+
}
293+
279294
err.span_suggestion(
280295
span,
281296
&format!("try calling `{}` as a method", ident),
282-
format!("self.{}", path_str),
297+
format!("self.{}({})", path_str, args_snippet),
283298
Applicability::MachineApplicable,
284299
);
285300
return (err, candidates);

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

+3-3
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)