Rollup merge of #68981 - estebank:silence, r=davidtwco
Account for type params on method without parentheses Account for those type parameters in the structured suggestion when forgetting to call method: ``` error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>` --> $DIR/method-missing-parentheses.rs:2:32 | LL | let _ = vec![].into_iter().collect::<usize>; | ^^^^^^^--------- | | | help: use parentheses to call the method: `collect::<usize>()` ```
This commit is contained in:
commit
2a3c1a30c8
7 changed files with 44 additions and 14 deletions
|
@ -823,7 +823,7 @@ impl<'a> Parser<'a> {
|
|||
if let Some(args) = segment.args {
|
||||
self.struct_span_err(
|
||||
args.span(),
|
||||
"field expressions may not have generic arguments",
|
||||
"field expressions cannot have generic arguments",
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
|
|
|
@ -1586,7 +1586,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
&format!("a method `{}` also exists, call it with parentheses", field),
|
||||
field,
|
||||
expr_t,
|
||||
expr.hir_id,
|
||||
expr,
|
||||
);
|
||||
}
|
||||
err.emit();
|
||||
|
@ -1609,7 +1609,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
"use parentheses to call the method",
|
||||
field,
|
||||
expr_t,
|
||||
expr.hir_id,
|
||||
expr,
|
||||
);
|
||||
} else {
|
||||
err.help("methods are immutable and cannot be assigned to");
|
||||
|
|
|
@ -135,7 +135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
msg: &str,
|
||||
method_name: ast::Ident,
|
||||
self_ty: Ty<'tcx>,
|
||||
call_expr_id: hir::HirId,
|
||||
call_expr: &hir::Expr<'_>,
|
||||
) {
|
||||
let has_params = self
|
||||
.probe_for_name(
|
||||
|
@ -144,7 +144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
method_name,
|
||||
IsSuggestion(false),
|
||||
self_ty,
|
||||
call_expr_id,
|
||||
call_expr.hir_id,
|
||||
ProbeScope::TraitsInScope,
|
||||
)
|
||||
.and_then(|pick| {
|
||||
|
@ -152,13 +152,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
Ok(sig.inputs().skip_binder().len() > 1)
|
||||
});
|
||||
|
||||
// Account for `foo.bar<T>`;
|
||||
let sugg_span = method_name.span.with_hi(call_expr.span.hi());
|
||||
let snippet = self
|
||||
.tcx
|
||||
.sess
|
||||
.source_map()
|
||||
.span_to_snippet(sugg_span)
|
||||
.unwrap_or_else(|_| method_name.to_string());
|
||||
let (suggestion, applicability) = if has_params.unwrap_or_default() {
|
||||
(format!("{}(...)", method_name), Applicability::HasPlaceholders)
|
||||
(format!("{}(...)", snippet), Applicability::HasPlaceholders)
|
||||
} else {
|
||||
(format!("{}()", method_name), Applicability::MaybeIncorrect)
|
||||
(format!("{}()", snippet), Applicability::MaybeIncorrect)
|
||||
};
|
||||
|
||||
err.span_suggestion(method_name.span, msg, suggestion, applicability);
|
||||
err.span_suggestion(sugg_span, msg, suggestion, applicability);
|
||||
}
|
||||
|
||||
/// Performs method lookup. If lookup is successful, it will return the callee
|
||||
|
|
|
@ -9,9 +9,9 @@ fn main() {
|
|||
y: 2,
|
||||
};
|
||||
f.x::<isize>;
|
||||
//~^ ERROR field expressions may not have generic arguments
|
||||
//~^ ERROR field expressions cannot have generic arguments
|
||||
f.x::<>;
|
||||
//~^ ERROR field expressions may not have generic arguments
|
||||
//~^ ERROR field expressions cannot have generic arguments
|
||||
f.x::();
|
||||
//~^ ERROR field expressions may not have generic arguments
|
||||
//~^ ERROR field expressions cannot have generic arguments
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
error: field expressions may not have generic arguments
|
||||
error: field expressions cannot have generic arguments
|
||||
--> $DIR/type-parameters-in-field-exprs.rs:11:10
|
||||
|
|
||||
LL | f.x::<isize>;
|
||||
| ^^^^^^^
|
||||
|
||||
error: field expressions may not have generic arguments
|
||||
error: field expressions cannot have generic arguments
|
||||
--> $DIR/type-parameters-in-field-exprs.rs:13:10
|
||||
|
|
||||
LL | f.x::<>;
|
||||
| ^^
|
||||
|
||||
error: field expressions may not have generic arguments
|
||||
error: field expressions cannot have generic arguments
|
||||
--> $DIR/type-parameters-in-field-exprs.rs:15:7
|
||||
|
|
||||
LL | f.x::();
|
||||
|
|
5
src/test/ui/suggestions/method-missing-parentheses.rs
Normal file
5
src/test/ui/suggestions/method-missing-parentheses.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
let _ = vec![].into_iter().collect::<usize>;
|
||||
//~^ ERROR attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
|
||||
//~| ERROR field expressions cannot have generic arguments
|
||||
}
|
17
src/test/ui/suggestions/method-missing-parentheses.stderr
Normal file
17
src/test/ui/suggestions/method-missing-parentheses.stderr
Normal file
|
@ -0,0 +1,17 @@
|
|||
error: field expressions cannot have generic arguments
|
||||
--> $DIR/method-missing-parentheses.rs:2:41
|
||||
|
|
||||
LL | let _ = vec![].into_iter().collect::<usize>;
|
||||
| ^^^^^^^
|
||||
|
||||
error[E0615]: attempted to take value of method `collect` on type `std::vec::IntoIter<_>`
|
||||
--> $DIR/method-missing-parentheses.rs:2:32
|
||||
|
|
||||
LL | let _ = vec![].into_iter().collect::<usize>;
|
||||
| ^^^^^^^---------
|
||||
| |
|
||||
| help: use parentheses to call the method: `collect::<usize>()`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0615`.
|
Loading…
Add table
Add a link
Reference in a new issue