]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/clone_on_ref_ptr.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / clone_on_ref_ptr.rs
CommitLineData
cdc7bbd5
XL
1use clippy_utils::diagnostics::span_lint_and_sugg;
2use clippy_utils::paths;
3use clippy_utils::source::snippet_with_macro_callsite;
4use clippy_utils::ty::{is_type_diagnostic_item, match_type};
f20569fa
XL
5use rustc_errors::Applicability;
6use rustc_hir as hir;
7use rustc_lint::LateContext;
8use rustc_middle::ty;
cdc7bbd5 9use rustc_span::symbol::{sym, Symbol};
f20569fa
XL
10
11use super::CLONE_ON_REF_PTR;
12
f2b60f7d
FG
13pub(super) fn check(
14 cx: &LateContext<'_>,
15 expr: &hir::Expr<'_>,
16 method_name: Symbol,
17 receiver: &hir::Expr<'_>,
18 args: &[hir::Expr<'_>],
19) {
20 if !(args.is_empty() && method_name == sym::clone) {
cdc7bbd5
XL
21 return;
22 }
f2b60f7d 23 let obj_ty = cx.typeck_results().expr_ty(receiver).peel_refs();
f20569fa
XL
24
25 if let ty::Adt(_, subst) = obj_ty.kind() {
26 let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
27 "Rc"
28 } else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
29 "Arc"
30 } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
31 "Weak"
32 } else {
33 return;
34 };
35
f2b60f7d 36 let snippet = snippet_with_macro_callsite(cx, receiver.span, "..");
f20569fa
XL
37
38 span_lint_and_sugg(
39 cx,
40 CLONE_ON_REF_PTR,
41 expr.span,
42 "using `.clone()` on a ref-counted pointer",
43 "try this",
44 format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet),
45 Applicability::Unspecified, // Sometimes unnecessary ::<_> after Rc/Arc/Weak
46 );
47 }
48}