提交 09185394 编写于 作者: J Janusz Marcinkiewicz

Add arguments to suggestion method call

上级 8e5b2c80
......@@ -262,6 +262,7 @@ pub(crate) fn smart_resolve_report_errors(
// 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 @@ pub(crate) fn smart_resolve_report_errors(
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 @@ pub(crate) fn smart_resolve_report_errors(
};
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);
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册