Skip to content

Commit 02fc256

Browse files
committed
Add should_call_clone_as_function() utility function
1 parent 4a8c949 commit 02fc256

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

clippy_lints/src/methods/map_clone.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
4+
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, should_call_clone_as_function};
55
use clippy_utils::{is_diag_trait_item, match_def_path, paths, peel_blocks};
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -124,11 +124,7 @@ fn handle_path(
124124
&& let ty::Ref(_, ty, Mutability::Not) = ty.kind()
125125
&& let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind()
126126
&& lst.iter().all(|l| l.as_type() == Some(*ty))
127-
&& !matches!(
128-
ty.ty_adt_def()
129-
.and_then(|adt_def| cx.tcx.get_diagnostic_name(adt_def.did())),
130-
Some(sym::Arc | sym::ArcWeak | sym::Rc | sym::RcWeak)
131-
)
127+
&& !should_call_clone_as_function(cx, *ty)
132128
{
133129
lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs()));
134130
}

clippy_utils/src/ty.rs

+10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ pub fn get_type_diagnostic_name(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<Symb
159159
}
160160
}
161161

162+
/// Returns true if `ty` is a type on which calling `Clone` through a function instead of
163+
/// as a method, such as `Arc::clone()` is considered idiomatic. Lints should avoid suggesting to
164+
/// replace instances of `ty::Clone()` by `.clone()` for objects of those types.
165+
pub fn should_call_clone_as_function(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
166+
matches!(
167+
get_type_diagnostic_name(cx, ty),
168+
Some(sym::Arc | sym::ArcWeak | sym::Rc | sym::RcWeak)
169+
)
170+
}
171+
162172
/// Returns true if ty has `iter` or `iter_mut` methods
163173
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
164174
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter`

0 commit comments

Comments
 (0)