Skip to content

Commit 5a1c454

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 5a1c454

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -3961,6 +3961,23 @@ 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.map_bound(|pred| pred.with_self_ty(tcx, ty))
3968+
&& self
3969+
.predicate_may_hold(&Obligation::misc(tcx, expr.span, body_id, param_env, pred))
3970+
{
3971+
err.span_suggestion_verbose(
3972+
expr.span.with_lo(rcvr.span.hi()),
3973+
format!(
3974+
"consider removing this method call, as the receiver has type `{ty}` and \
3975+
`{pred}` trivially holds",
3976+
),
3977+
"",
3978+
Applicability::MaybeIncorrect,
3979+
);
3980+
}
39643981
if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
39653982
let inner_expr = expr.peel_blocks();
39663983
let ty = typeck_results
@@ -4096,7 +4113,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
40964113
}
40974114
}
40984115

4099-
if let Node::Expr(expr) = tcx.hir_node(call_hir_id) {
4116+
if let Node::Expr(expr) = call_node {
41004117
if let hir::ExprKind::Call(hir::Expr { span, .. }, _)
41014118
| hir::ExprKind::MethodCall(
41024119
hir::PathSegment { ident: Ident { span, .. }, .. },

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>` trivially holds
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

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` trivially holds
15+
|
16+
LL - check_bound("nocopy".to_string());
17+
LL + check_bound("nocopy");
18+
|
1419

1520
error: aborting due to 1 previous error
1621

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` trivially holds
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)