]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/clone_on_ref_ptr.rs
7098d564cfc8980d448668c7b2cdb047635d72c0
[rust.git] / src / tools / clippy / clippy_lints / src / methods / clone_on_ref_ptr.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::paths;
3 use clippy_utils::source::snippet_with_macro_callsite;
4 use clippy_utils::ty::{is_type_diagnostic_item, match_type};
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8 use rustc_middle::ty;
9 use rustc_span::symbol::{sym, Symbol};
10
11 use super::CLONE_ON_REF_PTR;
12
13 pub(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) {
21         return;
22     }
23     let arg = receiver;
24     let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
25
26     if let ty::Adt(_, subst) = obj_ty.kind() {
27         let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
28             "Rc"
29         } else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
30             "Arc"
31         } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
32             "Weak"
33         } else {
34             return;
35         };
36
37         let snippet = snippet_with_macro_callsite(cx, arg.span, "..");
38
39         span_lint_and_sugg(
40             cx,
41             CLONE_ON_REF_PTR,
42             expr.span,
43             "using `.clone()` on a ref-counted pointer",
44             "try this",
45             format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet),
46             Applicability::Unspecified, // Sometimes unnecessary ::<_> after Rc/Arc/Weak
47         );
48     }
49 }