comments feedback

This commit is contained in:
yukang 2023-01-06 07:44:10 +08:00
parent 1a0a6132a8
commit ce4afed2ef

View File

@ -211,10 +211,13 @@ fn suggest_add_clone_to_arg(
obligation: &PredicateObligation<'tcx>, obligation: &PredicateObligation<'tcx>,
err: &mut Diagnostic, err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
); ) -> bool;
fn suggest_add_reference_to_arg( fn suggest_add_reference_to_arg(
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diagnostic, err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>,
has_custom_message: bool, has_custom_message: bool,
) -> bool; ) -> bool;
@ -1112,60 +1115,58 @@ fn suggest_add_clone_to_arg(
err: &mut Diagnostic, err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool { ) -> bool {
let span = obligation.cause.span;
let body_id = obligation.cause.body_id;
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty()); let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
let ty = self.tcx.erase_late_bound_regions(self_ty); let ty = self.tcx.erase_late_bound_regions(self_ty);
let owner = self.tcx.hir().get_parent_item(body_id); let owner = self.tcx.hir().get_parent_item(obligation.cause.body_id);
if let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() && let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false };
let arg_node = self.tcx.hir().get(*arg_hir_id) && let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node && let ty::Param(param) = inner_ty.kind() else { return false };
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) && let Some(generic_param) = generics.get_named(param.name) else { return false };
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() && let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false };
let ty::Param(param) = inner_ty.kind() && let arg_node = self.tcx.hir().get(*arg_hir_id);
let Some(generic_param) = let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node else { return false };
generics.params.iter().find(|p| p.name.ident().as_str() == param.name.as_str())
{
let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None);
let has_clone = self
.type_implements_trait(clone_trait, [ty], obligation.param_env)
.must_apply_modulo_regions();
let trait_pred_and_suggested_ty = let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None);
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)); let has_clone = self
let new_obligation = self.mk_trait_obligation_with_new_self_ty( .type_implements_trait(clone_trait, [ty], obligation.param_env)
obligation.param_env, .must_apply_modulo_regions();
trait_pred_and_suggested_ty,
);
if has_clone && self.predicate_may_hold(&new_obligation) { let trait_pred_and_suggested_ty =
let clone_bound = generics.bounds_for_param(generic_param.def_id) trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty));
.flat_map(|bp| bp.bounds) let new_obligation = self.mk_trait_obligation_with_new_self_ty(
.any(|bound| { obligation.param_env,
if let hir::GenericBound::Trait( hir::PolyTraitRef { trait_ref, ..}, ..) = bound { trait_pred_and_suggested_ty,
Some(clone_trait) == trait_ref.trait_def_id() );
} else {
false if has_clone && self.predicate_may_hold(&new_obligation) {
} let clone_bound = generics
}); .bounds_for_param(generic_param.def_id)
if !clone_bound { .flat_map(|bp| bp.bounds)
suggest_constraining_type_param( .any(|bound| {
self.tcx, if let hir::GenericBound::Trait(hir::PolyTraitRef { trait_ref, .. }, ..) = bound
generics, {
err, Some(clone_trait) == trait_ref.trait_def_id()
param.name.as_str(), } else {
"Clone", false
Some(clone_trait) }
); });
} if !clone_bound {
err.span_suggestion_verbose( suggest_constraining_type_param(
span.shrink_to_hi(), self.tcx,
"consider using clone here", generics,
".clone()".to_string(), err,
Applicability::MaybeIncorrect, param.name.as_str(),
"Clone",
Some(clone_trait),
); );
return true;
} }
err.span_suggestion_verbose(
obligation.cause.span.shrink_to_hi(),
"consider using clone here",
".clone()".to_string(),
Applicability::MaybeIncorrect,
);
return true;
} }
false false
} }