1 use super::implicit_clone::is_clone_like;
2 use super::unnecessary_iter_cloned::{self, is_into_iter};
3 use clippy_utils::diagnostics::span_lint_and_sugg;
4 use clippy_utils::source::snippet_opt;
5 use clippy_utils::ty::{get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs};
6 use clippy_utils::visitors::find_all_ret_expressions;
7 use clippy_utils::{fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, return_ty};
8 use clippy_utils::{meets_msrv, msrvs};
9 use rustc_errors::Applicability;
10 use rustc_hir::{def_id::DefId, BorrowKind, Expr, ExprKind, ItemKind, Node};
11 use rustc_infer::infer::TyCtxtInferExt;
12 use rustc_lint::LateContext;
13 use rustc_middle::mir::Mutability;
14 use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
15 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
16 use rustc_middle::ty::EarlyBinder;
17 use rustc_middle::ty::{self, ParamTy, PredicateKind, ProjectionPredicate, TraitPredicate, Ty};
18 use rustc_semver::RustcVersion;
19 use rustc_span::{sym, Symbol};
20 use rustc_trait_selection::traits::{query::evaluate_obligation::InferCtxtExt as _, Obligation, ObligationCause};
21 use rustc_typeck::check::{FnCtxt, Inherited};
24 use super::UNNECESSARY_TO_OWNED;
27 cx: &LateContext<'tcx>,
28 expr: &'tcx Expr<'tcx>,
30 receiver: &'tcx Expr<'_>,
31 args: &'tcx [Expr<'_>],
32 msrv: Option<RustcVersion>,
35 if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
38 if is_cloned_or_copied(cx, method_name, method_def_id) {
39 unnecessary_iter_cloned::check(cx, expr, method_name, receiver);
40 } else if is_to_owned_like(cx, expr, method_name, method_def_id) {
41 // At this point, we know the call is of a `to_owned`-like function. The functions
42 // `check_addr_of_expr` and `check_call_arg` determine whether the call is unnecessary
43 // based on its context, that is, whether it is a referent in an `AddrOf` expression, an
44 // argument in a `into_iter` call, or an argument in the call of some other function.
45 if check_addr_of_expr(cx, expr, method_name, method_def_id, receiver) {
48 if check_into_iter_call_arg(cx, expr, method_name, receiver, msrv) {
51 check_other_call_arg(cx, expr, method_name, receiver);
57 /// Checks whether `expr` is a referent in an `AddrOf` expression and, if so, determines whether its
58 /// call of a `to_owned`-like function is unnecessary.
59 #[allow(clippy::too_many_lines)]
60 fn check_addr_of_expr(
68 if let Some(parent) = get_parent_expr(cx, expr);
69 if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, _) = parent.kind;
70 let adjustments = cx.typeck_results().expr_adjustments(parent).iter().collect::<Vec<_>>();
72 // For matching uses of `Cow::from`
75 kind: Adjust::Deref(None),
79 kind: Adjust::Borrow(_),
83 // For matching uses of arrays
86 kind: Adjust::Deref(None),
90 kind: Adjust::Borrow(_),
94 kind: Adjust::Pointer(_),
98 // For matching everything else
101 kind: Adjust::Deref(None),
105 kind: Adjust::Deref(Some(OverloadedDeref { .. })),
109 kind: Adjust::Borrow(_),
113 let receiver_ty = cx.typeck_results().expr_ty(receiver);
114 let (target_ty, n_target_refs) = peel_mid_ty_refs(*target_ty);
115 let (receiver_ty, n_receiver_refs) = peel_mid_ty_refs(receiver_ty);
116 // Only flag cases satisfying at least one of the following three conditions:
117 // * the referent and receiver types are distinct
118 // * the referent/receiver type is a copyable array
119 // * the method is `Cow::into_owned`
120 // This restriction is to ensure there is no overlap between `redundant_clone` and this
121 // lint. It also avoids the following false positive:
122 // https://github.com/rust-lang/rust-clippy/issues/8759
123 // Arrays are a bit of a corner case. Non-copyable arrays are handled by
124 // `redundant_clone`, but copyable arrays are not.
125 if *referent_ty != receiver_ty
126 || (matches!(referent_ty.kind(), ty::Array(..)) && is_copy(cx, *referent_ty))
127 || is_cow_into_owned(cx, method_name, method_def_id);
128 if let Some(receiver_snippet) = snippet_opt(cx, receiver.span);
130 if receiver_ty == target_ty && n_target_refs >= n_receiver_refs {
133 UNNECESSARY_TO_OWNED,
135 &format!("unnecessary use of `{}`", method_name),
141 width = n_target_refs - n_receiver_refs
143 Applicability::MachineApplicable,
148 if let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref);
149 if implements_trait(cx, receiver_ty, deref_trait_id, &[]);
150 if get_associated_type(cx, receiver_ty, deref_trait_id, "Target") == Some(target_ty);
152 if n_receiver_refs > 0 {
155 UNNECESSARY_TO_OWNED,
157 &format!("unnecessary use of `{}`", method_name),
160 Applicability::MachineApplicable,
165 UNNECESSARY_TO_OWNED,
166 expr.span.with_lo(receiver.span.hi()),
167 &format!("unnecessary use of `{}`", method_name),
170 Applicability::MachineApplicable,
177 if let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef);
178 if implements_trait(cx, receiver_ty, as_ref_trait_id, &[GenericArg::from(target_ty)]);
182 UNNECESSARY_TO_OWNED,
184 &format!("unnecessary use of `{}`", method_name),
186 format!("{}.as_ref()", receiver_snippet),
187 Applicability::MachineApplicable,
197 /// Checks whether `expr` is an argument in an `into_iter` call and, if so, determines whether its
198 /// call of a `to_owned`-like function is unnecessary.
199 fn check_into_iter_call_arg(
200 cx: &LateContext<'_>,
204 msrv: Option<RustcVersion>,
207 if let Some(parent) = get_parent_expr(cx, expr);
208 if let Some(callee_def_id) = fn_def_id(cx, parent);
209 if is_into_iter(cx, callee_def_id);
210 if let Some(iterator_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
211 let parent_ty = cx.typeck_results().expr_ty(parent);
212 if implements_trait(cx, parent_ty, iterator_trait_id, &[]);
213 if let Some(item_ty) = get_iterator_item_ty(cx, parent_ty);
214 if let Some(receiver_snippet) = snippet_opt(cx, receiver.span);
216 if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
219 let cloned_or_copied = if is_copy(cx, item_ty) && meets_msrv(msrv, msrvs::ITERATOR_COPIED) {
224 // The next suggestion may be incorrect because the removal of the `to_owned`-like
225 // function could cause the iterator to hold a reference to a resource that is used
226 // mutably. See https://github.com/rust-lang/rust-clippy/issues/8148.
229 UNNECESSARY_TO_OWNED,
231 &format!("unnecessary use of `{}`", method_name),
233 format!("{}.iter().{}()", receiver_snippet, cloned_or_copied),
234 Applicability::MaybeIncorrect,
242 /// Checks whether `expr` is an argument in a function call and, if so, determines whether its call
243 /// of a `to_owned`-like function is unnecessary.
244 fn check_other_call_arg<'tcx>(
245 cx: &LateContext<'tcx>,
246 expr: &'tcx Expr<'tcx>,
248 receiver: &'tcx Expr<'tcx>,
251 if let Some((maybe_call, maybe_arg)) = skip_addr_of_ancestors(cx, expr);
252 if let Some((callee_def_id, _, recv, call_args)) = get_callee_substs_and_args(cx, maybe_call);
253 let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder();
254 if let Some(i) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == maybe_arg.hir_id);
255 if let Some(input) = fn_sig.inputs().get(i);
256 let (input, n_refs) = peel_mid_ty_refs(*input);
257 if let (trait_predicates, _) = get_input_traits_and_projections(cx, callee_def_id, input);
258 if let Some(sized_def_id) = cx.tcx.lang_items().sized_trait();
259 if let [trait_predicate] = trait_predicates
261 .filter(|trait_predicate| trait_predicate.def_id() != sized_def_id)
262 .collect::<Vec<_>>()[..];
263 if let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref);
264 if let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef);
265 if trait_predicate.def_id() == deref_trait_id || trait_predicate.def_id() == as_ref_trait_id;
266 let receiver_ty = cx.typeck_results().expr_ty(receiver);
267 if can_change_type(cx, maybe_arg, receiver_ty);
268 // We can't add an `&` when the trait is `Deref` because `Target = &T` won't match
270 if n_refs > 0 || is_copy(cx, receiver_ty) || trait_predicate.def_id() != deref_trait_id;
271 let n_refs = max(n_refs, if is_copy(cx, receiver_ty) { 0 } else { 1 });
272 if let Some(receiver_snippet) = snippet_opt(cx, receiver.span);
276 UNNECESSARY_TO_OWNED,
278 &format!("unnecessary use of `{}`", method_name),
280 format!("{:&>width$}{}", "", receiver_snippet, width = n_refs),
281 Applicability::MachineApplicable,
289 /// Walks an expression's ancestors until it finds a non-`AddrOf` expression. Returns the first such
290 /// expression found (if any) along with the immediately prior expression.
291 fn skip_addr_of_ancestors<'tcx>(
292 cx: &LateContext<'tcx>,
293 mut expr: &'tcx Expr<'tcx>,
294 ) -> Option<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>)> {
295 while let Some(parent) = get_parent_expr(cx, expr) {
296 if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, _) = parent.kind {
299 return Some((parent, expr));
305 /// Checks whether an expression is a function or method call and, if so, returns its `DefId`,
306 /// `Substs`, and arguments.
307 fn get_callee_substs_and_args<'tcx>(
308 cx: &LateContext<'tcx>,
309 expr: &'tcx Expr<'tcx>,
310 ) -> Option<(DefId, SubstsRef<'tcx>, Option<&'tcx Expr<'tcx>>, &'tcx [Expr<'tcx>])> {
312 if let ExprKind::Call(callee, args) = expr.kind;
313 let callee_ty = cx.typeck_results().expr_ty(callee);
314 if let ty::FnDef(callee_def_id, _) = callee_ty.kind();
316 let substs = cx.typeck_results().node_substs(callee.hir_id);
317 return Some((*callee_def_id, substs, None, args));
321 if let ExprKind::MethodCall(_, recv, args, _) = expr.kind;
322 if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
324 let substs = cx.typeck_results().node_substs(expr.hir_id);
325 return Some((method_def_id, substs, Some(recv), args));
331 /// Returns the `TraitPredicate`s and `ProjectionPredicate`s for a function's input type.
332 fn get_input_traits_and_projections<'tcx>(
333 cx: &LateContext<'tcx>,
334 callee_def_id: DefId,
336 ) -> (Vec<TraitPredicate<'tcx>>, Vec<ProjectionPredicate<'tcx>>) {
337 let mut trait_predicates = Vec::new();
338 let mut projection_predicates = Vec::new();
339 for predicate in cx.tcx.param_env(callee_def_id).caller_bounds() {
340 match predicate.kind().skip_binder() {
341 PredicateKind::Trait(trait_predicate) => {
342 if trait_predicate.trait_ref.self_ty() == input {
343 trait_predicates.push(trait_predicate);
346 PredicateKind::Projection(projection_predicate) => {
347 if projection_predicate.projection_ty.self_ty() == input {
348 projection_predicates.push(projection_predicate);
354 (trait_predicates, projection_predicates)
357 fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<'a>) -> bool {
358 for (_, node) in cx.tcx.hir().parent_iter(expr.hir_id) {
360 Node::Stmt(_) => return true,
361 Node::Block(..) => continue,
362 Node::Item(item) => {
363 if let ItemKind::Fn(_, _, body_id) = &item.kind
364 && let output_ty = return_ty(cx, item.hir_id())
365 && let local_def_id = cx.tcx.hir().local_def_id(item.hir_id())
366 && Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
367 let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, item.hir_id());
368 fn_ctxt.can_coerce(ty, output_ty)
370 if has_lifetime(output_ty) && has_lifetime(ty) {
373 let body = cx.tcx.hir().body(*body_id);
374 let body_expr = &body.value;
376 return find_all_ret_expressions(cx, body_expr, |_| { count += 1; count <= 1 });
379 Node::Expr(parent_expr) => {
380 if let Some((callee_def_id, call_substs, recv, call_args)) = get_callee_substs_and_args(cx, parent_expr)
382 let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder();
383 if let Some(arg_index) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == expr.hir_id)
384 && let Some(param_ty) = fn_sig.inputs().get(arg_index)
385 && let ty::Param(ParamTy { index: param_index , ..}) = param_ty.kind()
391 .filter(|(i, _)| *i != arg_index)
392 .any(|(_, ty)| ty.contains(*param_ty))
397 let mut trait_predicates = cx.tcx.param_env(callee_def_id)
398 .caller_bounds().iter().filter(|predicate| {
399 if let PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder()
400 && trait_predicate.trait_ref.self_ty() == *param_ty {
407 let new_subst = cx.tcx.mk_substs(
411 if i == (*param_index as usize) {
417 if trait_predicates.any(|predicate| {
418 let predicate = EarlyBinder(predicate).subst(cx.tcx, new_subst);
419 let obligation = Obligation::new(ObligationCause::dummy(), cx.param_env, predicate);
422 .enter(|infcx| infcx.predicate_must_hold_modulo_regions(&obligation))
427 let output_ty = fn_sig.output();
428 if output_ty.contains(*param_ty) {
429 if let Ok(new_ty) = cx.tcx.try_subst_and_normalize_erasing_regions(
430 new_subst, cx.param_env, output_ty) {
440 } else if let ExprKind::Block(..) = parent_expr.kind {
452 fn has_lifetime(ty: Ty<'_>) -> bool {
453 ty.walk().any(|t| matches!(t.unpack(), GenericArgKind::Lifetime(_)))
456 /// Returns true if the named method is `Iterator::cloned` or `Iterator::copied`.
457 fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id: DefId) -> bool {
458 (method_name.as_str() == "cloned" || method_name.as_str() == "copied")
459 && is_diag_trait_item(cx, method_def_id, sym::Iterator)
462 /// Returns true if the named method can be used to convert the receiver to its "owned"
464 fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name: Symbol, method_def_id: DefId) -> bool {
465 is_clone_like(cx, method_name.as_str(), method_def_id)
466 || is_cow_into_owned(cx, method_name, method_def_id)
467 || is_to_string_on_string_like(cx, call_expr, method_name, method_def_id)
470 /// Returns true if the named method is `Cow::into_owned`.
471 fn is_cow_into_owned(cx: &LateContext<'_>, method_name: Symbol, method_def_id: DefId) -> bool {
472 method_name.as_str() == "into_owned" && is_diag_item_method(cx, method_def_id, sym::Cow)
475 /// Returns true if the named method is `ToString::to_string` and it's called on a type that
476 /// is string-like i.e. implements `AsRef<str>` or `Deref<str>`.
477 fn is_to_string_on_string_like<'a>(
478 cx: &LateContext<'_>,
479 call_expr: &'a Expr<'a>,
481 method_def_id: DefId,
483 if method_name != sym::to_string || !is_diag_trait_item(cx, method_def_id, sym::ToString) {
487 if let Some(substs) = cx.typeck_results().node_substs_opt(call_expr.hir_id)
488 && let [generic_arg] = substs.as_slice()
489 && let GenericArgKind::Type(ty) = generic_arg.unpack()
490 && let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref)
491 && let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef)
492 && (implements_trait(cx, ty, deref_trait_id, &[cx.tcx.types.str_.into()]) ||
493 implements_trait(cx, ty, as_ref_trait_id, &[cx.tcx.types.str_.into()])) {