Skip to content

Commit 9a1d514

Browse files
committed
Detect when method call on argument could be removed to fulfill failed trait bound
When encountering ```rust struct Foo; struct Bar; impl From<Bar> for Foo { fn from(_: Bar) -> Self { Foo } } fn qux(_: impl From<Bar>) {} fn main() { qux(Bar.into()); } ``` Suggest removing `.into()`: ``` error[E0283]: type annotations needed --> f100.rs:8:13 | 8 | qux(Bar.into()); | --- ^^^^ | | | required by a bound introduced by this call | = note: cannot satisfy `_: From<Bar>` note: required by a bound in `qux` --> f100.rs:6:16 | 6 | fn qux(_: impl From<Bar>) {} | ^^^^^^^^^ required by this bound in `qux` help: try using a fully qualified path to specify the expected types | 8 | qux(<Bar as Into<T>>::into(Bar)); | +++++++++++++++++++++++ ~ help: consider removing this method call, as the receiver has type `Bar` and `Bar: From<Bar>` can be fulfilled | 8 - qux(Bar.into()); 8 + qux(Bar); | ``` Fix #71252
1 parent eaff1af commit 9a1d514

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -3961,6 +3961,26 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
39613961
if let Node::Expr(expr) = tcx.hir_node(arg_hir_id)
39623962
&& let Some(typeck_results) = &self.typeck_results
39633963
{
3964+
if let hir::Expr { kind: hir::ExprKind::MethodCall(_, rcvr, _, _), .. } = expr
3965+
&& let Some(ty) = typeck_results.node_type_opt(rcvr.hir_id)
3966+
&& let Some(failed_pred) = failed_pred.to_opt_poly_trait_pred()
3967+
&& let pred = failed_pred.skip_binder().with_self_ty(tcx, ty)
3968+
&& self
3969+
.evaluate_obligation_no_overflow(&Obligation::misc(
3970+
tcx, expr.span, body_id, param_env, pred,
3971+
))
3972+
.must_apply_modulo_regions()
3973+
{
3974+
err.span_suggestion_verbose(
3975+
expr.span.with_lo(rcvr.span.hi()),
3976+
format!(
3977+
"consider removing this method call, as the receiver has type `{ty}` and \
3978+
`{pred}` can be fulfilled",
3979+
),
3980+
"",
3981+
Applicability::MaybeIncorrect,
3982+
);
3983+
}
39643984
if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
39653985
let inner_expr = expr.peel_blocks();
39663986
let ty = typeck_results
@@ -4096,7 +4116,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
40964116
}
40974117
}
40984118

4099-
if let Node::Expr(expr) = tcx.hir_node(call_hir_id) {
4119+
if let Node::Expr(expr) = call_node {
41004120
if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
41014121
| hir::ExprKind::MethodCall(
41024122
hir::PathSegment { ident: Ident { span, .. }, .. },

Diff for: tests/ui/async-await/issue-72442.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ LL | let mut f = File::open(path.to_str())?;
88
|
99
note: required by a bound in `File::open`
1010
--> $SRC_DIR/std/src/fs.rs:LL:COL
11+
help: consider removing this method call, as the receiver has type `&Path` and `&Path: AsRef<Path>` can be fulfilled
12+
|
13+
LL - let mut f = File::open(path.to_str())?;
14+
LL + let mut f = File::open(path)?;
15+
|
1116

1217
error: aborting due to 1 previous error
1318

Diff for: tests/ui/error-should-say-copy-not-pod.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ note: required by a bound in `check_bound`
1111
|
1212
LL | fn check_bound<T:Copy>(_: T) {}
1313
| ^^^^ required by this bound in `check_bound`
14+
help: consider removing this method call, as the receiver has type `&'static str` and `&'static str: Copy` can be fulfilled
15+
|
16+
LL - check_bound("nocopy".to_string());
17+
LL + check_bound("nocopy");
18+
|
1419

1520
error: aborting due to 1 previous error
1621

Diff for: tests/ui/suggestions/issue-84973-blacklist.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ note: required by a bound in `f_copy`
1111
|
1212
LL | fn f_copy<T: Copy>(t: T) {}
1313
| ^^^^ required by this bound in `f_copy`
14+
help: consider removing this method call, as the receiver has type `&'static str` and `&'static str: Copy` can be fulfilled
15+
|
16+
LL - f_copy("".to_string());
17+
LL + f_copy("");
18+
|
1419

1520
error[E0277]: the trait bound `S: Clone` is not satisfied
1621
--> $DIR/issue-84973-blacklist.rs:16:13

0 commit comments

Comments
 (0)