diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2024-03-23 00:23:59 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2024-03-23 09:59:14 +0100 |
| commit | 02fc25635e88b670f0b4bfe5afdcf4ab2771d3e0 (patch) | |
| tree | 0980b4c5355d5c2ffaf88f6f6f4ad027672d60d5 | |
| parent | 4a8c9495ca72ffc4e25ec75984d5678c5b9ae796 (diff) | |
| download | rust-02fc25635e88b670f0b4bfe5afdcf4ab2771d3e0.tar.gz rust-02fc25635e88b670f0b4bfe5afdcf4ab2771d3e0.zip | |
Add `should_call_clone_as_function()` utility function
| -rw-r--r-- | clippy_lints/src/methods/map_clone.rs | 8 | ||||
| -rw-r--r-- | 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 0ba8cbbe699..9d020092ccb 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 a2595580eaf..3924eb5a81c 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<Symb } } +/// Returns true if `ty` is a type on which calling `Clone` through a function instead of +/// as a method, such as `Arc::clone()` is considered idiomatic. Lints should avoid suggesting to +/// replace instances of `ty::Clone()` by `.clone()` for objects of those types. +pub fn should_call_clone_as_function(cx: &LateContext<'_>, 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<Symbol> { // FIXME: instead of this hard-coded list, we should check if `<adt>::iter` |
