Add should_call_clone_as_function() utility function

This commit is contained in:
Samuel Tardieu 2024-03-23 00:23:59 +01:00
parent 4a8c9495ca
commit 02fc25635e
2 changed files with 12 additions and 6 deletions

View File

@ -1,7 +1,7 @@
use clippy_config::msrvs::{self, Msrv}; use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability; 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 clippy_utils::{is_diag_trait_item, match_def_path, paths, peel_blocks};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
@ -124,11 +124,7 @@ fn handle_path(
&& let ty::Ref(_, ty, Mutability::Not) = ty.kind() && let ty::Ref(_, ty, Mutability::Not) = ty.kind()
&& let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind() && let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind()
&& lst.iter().all(|l| l.as_type() == Some(*ty)) && lst.iter().all(|l| l.as_type() == Some(*ty))
&& !matches!( && !should_call_clone_as_function(cx, *ty)
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)
)
{ {
lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs())); lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs()));
} }

View File

@ -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 /// Returns true if ty has `iter` or `iter_mut` methods
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> { 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` // FIXME: instead of this hard-coded list, we should check if `<adt>::iter`