Rollup merge of #106497 - chenyukang:yukang/fix-106443-sugg-clone, r=estebank
Suggest using clone when we have &T and T implemented Clone Fixes #106443
This commit is contained in:
commit
eefc44b7e2
8 changed files with 200 additions and 1 deletions
|
@ -873,6 +873,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
);
|
||||
}
|
||||
|
||||
if self.suggest_add_clone_to_arg(&obligation, &mut err, trait_predicate) {
|
||||
err.emit();
|
||||
return;
|
||||
}
|
||||
|
||||
if self.suggest_impl_trait(&mut err, span, &obligation, trait_predicate) {
|
||||
err.emit();
|
||||
return;
|
||||
|
|
|
@ -10,7 +10,7 @@ use crate::infer::InferCtxt;
|
|||
use crate::traits::{NormalizeExt, ObligationCtxt};
|
||||
|
||||
use hir::def::CtorOf;
|
||||
use hir::HirId;
|
||||
use hir::{Expr, HirId};
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_errors::{
|
||||
|
@ -206,6 +206,13 @@ pub trait TypeErrCtxtExt<'tcx> {
|
|||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||
);
|
||||
|
||||
fn suggest_add_clone_to_arg(
|
||||
&self,
|
||||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut Diagnostic,
|
||||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||
) -> bool;
|
||||
|
||||
fn suggest_add_reference_to_arg(
|
||||
&self,
|
||||
obligation: &PredicateObligation<'tcx>,
|
||||
|
@ -1102,6 +1109,55 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn suggest_add_clone_to_arg(
|
||||
&self,
|
||||
obligation: &PredicateObligation<'tcx>,
|
||||
err: &mut Diagnostic,
|
||||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||
) -> bool {
|
||||
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
|
||||
let ty = self.tcx.erase_late_bound_regions(self_ty);
|
||||
let owner = self.tcx.hir().get_parent_item(obligation.cause.body_id);
|
||||
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false };
|
||||
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
|
||||
let ty::Param(param) = inner_ty.kind() else { return false };
|
||||
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false };
|
||||
let arg_node = self.tcx.hir().get(*arg_hir_id);
|
||||
let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node else { return false };
|
||||
|
||||
let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None);
|
||||
let has_clone = |ty| {
|
||||
self.type_implements_trait(clone_trait, [ty], obligation.param_env)
|
||||
.must_apply_modulo_regions()
|
||||
};
|
||||
|
||||
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
|
||||
obligation.param_env,
|
||||
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
|
||||
);
|
||||
|
||||
if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
|
||||
if !has_clone(param.to_ty(self.tcx)) {
|
||||
suggest_constraining_type_param(
|
||||
self.tcx,
|
||||
generics,
|
||||
err,
|
||||
param.name.as_str(),
|
||||
"Clone",
|
||||
Some(clone_trait),
|
||||
);
|
||||
}
|
||||
err.span_suggestion_verbose(
|
||||
obligation.cause.span.shrink_to_hi(),
|
||||
"consider using clone here",
|
||||
".clone()".to_string(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn suggest_add_reference_to_arg(
|
||||
&self,
|
||||
obligation: &PredicateObligation<'tcx>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue