Skip to content

Commit 578dbc7

Browse files
committed
Simplify predicate handling code
1 parent cd70a3d commit 578dbc7

File tree

1 file changed

+18
-45
lines changed

1 file changed

+18
-45
lines changed

clippy_lints/src/dereference.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,6 @@ fn ty_contains_infer(ty: &hir::Ty<'_>) -> bool {
947947
// If the conditions are met, returns `Some(Position::ImplArg(..))`; otherwise, returns `None`.
948948
// The "is copyable" condition is to avoid the case where removing the `&` means `e` would have to
949949
// be moved, but it cannot be.
950-
#[expect(clippy::too_many_lines)]
951950
fn needless_borrow_impl_arg_position<'tcx>(
952951
cx: &LateContext<'tcx>,
953952
parent: &Expr<'tcx>,
@@ -959,7 +958,6 @@ fn needless_borrow_impl_arg_position<'tcx>(
959958
let sized_trait_def_id = cx.tcx.lang_items().sized_trait();
960959

961960
let Some(callee_def_id) = fn_def_id(cx, parent) else { return Position::Other(precedence) };
962-
let callee_generics = cx.tcx.generics_of(callee_def_id);
963961
let substs_with_expr_ty = cx
964962
.typeck_results()
965963
.node_substs(if let ExprKind::Call(callee, _) = parent.kind {
@@ -1032,54 +1030,29 @@ fn needless_borrow_impl_arg_position<'tcx>(
10321030
return false;
10331031
}
10341032

1035-
let substs_with_expr_ty = substs_with_expr_ty.split_at(callee_generics.parent_count);
1036-
let substs_with_referent_ty = substs_with_expr_ty
1037-
.0
1038-
.iter()
1039-
.copied()
1040-
.chain(
1041-
callee_generics
1042-
.params
1043-
.iter()
1044-
.zip(substs_with_expr_ty.1.iter())
1045-
.map(|(param, arg)| {
1046-
if param.index == param_ty.index {
1047-
ty::GenericArg::from(referent_ty)
1048-
} else {
1049-
projection_predicates
1050-
.iter()
1051-
.find_map(|projection_predicate| {
1052-
if let ty::Term::Ty(term_ty) = projection_predicate.term
1053-
&& term_ty.is_param(param.index)
1054-
{
1055-
let item_def_id = projection_predicate.projection_ty.item_def_id;
1056-
let assoc_item = cx.tcx.associated_item(item_def_id);
1057-
let projection = cx
1058-
.tcx
1059-
.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(referent_ty, &[]));
1060-
let projected_ty = cx.tcx.normalize_erasing_regions(cx.param_env, projection);
1061-
Some(ty::GenericArg::from(projected_ty))
1062-
} else {
1063-
None
1064-
}
1065-
})
1066-
.unwrap_or(*arg)
1067-
}
1068-
}),
1069-
)
1070-
.collect::<Vec<_>>();
1033+
let mut substs_with_referent_ty = substs_with_expr_ty.to_vec();
1034+
substs_with_referent_ty[param_ty.index as usize] = ty::GenericArg::from(referent_ty);
1035+
for projection_predicate in &projection_predicates {
1036+
if let ty::Term::Ty(term_ty) = projection_predicate.term
1037+
&& let ty::Param(term_param_ty) = term_ty.kind()
1038+
{
1039+
let item_def_id = projection_predicate.projection_ty.item_def_id;
1040+
let assoc_item = cx.tcx.associated_item(item_def_id);
1041+
let projection = cx
1042+
.tcx
1043+
.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(referent_ty, &[]));
1044+
let projected_ty = cx.tcx.normalize_erasing_regions(cx.param_env, projection);
1045+
substs_with_referent_ty[term_param_ty.index as usize] = ty::GenericArg::from(projected_ty);
1046+
}
1047+
}
10711048

1072-
let predicates = EarlyBinder(predicates).subst(cx.tcx, &substs_with_referent_ty);
1073-
if !predicates.iter().all(|predicate| {
1049+
predicates.iter().all(|predicate| {
1050+
let predicate = EarlyBinder(predicate).subst(cx.tcx, &substs_with_referent_ty);
10741051
let obligation = Obligation::new(ObligationCause::dummy(), cx.param_env, predicate);
10751052
cx.tcx
10761053
.infer_ctxt()
10771054
.enter(|infcx| infcx.predicate_must_hold_modulo_regions(&obligation))
1078-
}) {
1079-
return false;
1080-
}
1081-
1082-
true
1055+
})
10831056
};
10841057

10851058
let mut needless_borrow = false;

0 commit comments

Comments
 (0)