From 02fc25635e88b670f0b4bfe5afdcf4ab2771d3e0 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sat, 23 Mar 2024 00:23:59 +0100 Subject: [PATCH] Add `should_call_clone_as_function()` utility function --- clippy_lints/src/methods/map_clone.rs | 8 ++------ clippy_utils/src/ty.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/methods/map_clone.rs b/clippy_lints/src/methods/map_clone.rs index 0ba8cbbe6..9d020092c 100644 --- a/clippy_lints/src/methods/map_clone.rs +++ b/clippy_lints/src/methods/map_clone.rs @@ -1,7 +1,7 @@ use clippy_config::msrvs::{self, Msrv}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::ty::{is_copy, is_type_diagnostic_item}; +use clippy_utils::ty::{is_copy, is_type_diagnostic_item, should_call_clone_as_function}; use clippy_utils::{is_diag_trait_item, match_def_path, paths, peel_blocks}; use rustc_errors::Applicability; use rustc_hir as hir; @@ -124,11 +124,7 @@ fn handle_path( && let ty::Ref(_, ty, Mutability::Not) = ty.kind() && let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind() && lst.iter().all(|l| l.as_type() == Some(*ty)) - && !matches!( - ty.ty_adt_def() - .and_then(|adt_def| cx.tcx.get_diagnostic_name(adt_def.did())), - Some(sym::Arc | sym::ArcWeak | sym::Rc | sym::RcWeak) - ) + && !should_call_clone_as_function(cx, *ty) { lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs())); } diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index a2595580e..3924eb5a8 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -159,6 +159,16 @@ pub fn get_type_diagnostic_name(cx: &LateContext<'_>, ty: Ty<'_>) -> Option, ty: Ty<'_>) -> bool { + matches!( + get_type_diagnostic_name(cx, ty), + Some(sym::Arc | sym::ArcWeak | sym::Rc | sym::RcWeak) + ) +} + /// Returns true if ty has `iter` or `iter_mut` methods pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option { // FIXME: instead of this hard-coded list, we should check if `::iter`