Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4069,15 +4069,46 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
))
&& expr.span.hi() != rcvr.span.hi()
{
err.span_suggestion_verbose(
expr.span.with_lo(rcvr.span.hi()),
format!(
"consider removing this method call, as the receiver has type `{ty}` and \
`{pred}` trivially holds",
),
"",
Applicability::MaybeIncorrect,
);
let should_sugg = match tcx.hir_node(call_hir_id) {
Node::Expr(hir::Expr {
kind: hir::ExprKind::MethodCall(_, call_receiver, _, _),
..
}) if let Some((DefKind::AssocFn, did)) =
typeck_results.type_dependent_def(call_hir_id)
&& call_receiver.hir_id == arg_hir_id =>
{
// Avoid suggesting removing a method call if the argument is the receiver of the parent call and
// removing the receiver would make the method inaccessible. i.e. `x.a().b()`, suggesting removing
// `.a()` could change the type and make `.b()` unavailable.
if tcx.inherent_impl_of_assoc(did).is_some() {
// if we're calling an inherent impl method, just try to make sure that the receiver type stays the same.
Some(ty) == typeck_results.node_type_opt(arg_hir_id)
} else {
// we're calling a trait method, so we just check removing the method call still satisfies the trait.
let trait_id = tcx
.trait_of_assoc(did)
.unwrap_or_else(|| tcx.impl_trait_id(tcx.parent(did)));
let args = typeck_results.node_args(call_hir_id);
let tr = ty::TraitRef::from_assoc(tcx, trait_id, args)
.with_replaced_self_ty(tcx, ty);
self.type_implements_trait(tr.def_id, tr.args, param_env)
.must_apply_modulo_regions()
}
}
_ => true,
};

if should_sugg {
err.span_suggestion_verbose(
expr.span.with_lo(rcvr.span.hi()),
format!(
"consider removing this method call, as the receiver has type `{ty}` and \
`{pred}` trivially holds",
),
"",
Applicability::MaybeIncorrect,
);
}
}
if let hir::Expr { kind: hir::ExprKind::Block(block, _), .. } = expr {
let inner_expr = expr.peel_blocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ fn main() {
//~| HELP try using a fully qualified path to specify the expected types
//~| HELP consider removing this method call, as the receiver has type `Bar` and `Bar: From<Bar>` trivially holds
}

// regression test for https://github.com/rust-lang/rust/issues/149487.
fn quux() {
let mut tx_heights: std::collections::BTreeMap<(), Option<()>> = <_>::default();
tx_heights.get(&()).unwrap_or_default();
//~^ ERROR the trait bound `&Option<()>: Default` is not satisfied
//~| HELP: the trait `Default` is implemented for `Option<T>`
}
16 changes: 14 additions & 2 deletions tests/ui/trait-bounds/argument-with-unnecessary-method-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ LL - qux(Bar.into());
LL + qux(Bar);
|

error: aborting due to 1 previous error
error[E0277]: the trait bound `&Option<()>: Default` is not satisfied
--> $DIR/argument-with-unnecessary-method-call.rs:16:25
|
LL | tx_heights.get(&()).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&Option<()>`
|
help: the trait `Default` is implemented for `Option<T>`
--> $SRC_DIR/core/src/option.rs:LL:COL
note: required by a bound in `Option::<T>::unwrap_or_default`
--> $SRC_DIR/core/src/option.rs:LL:COL

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0283`.
Some errors have detailed explanations: E0277, E0283.
For more information about an error, try `rustc --explain E0277`.
Loading