]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/clone_on_ref_ptr.rs
Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor
[rust.git] / src / tools / clippy / clippy_lints / src / methods / clone_on_ref_ptr.rs
1 use crate::utils::{is_type_diagnostic_item, match_type, paths, snippet_with_macro_callsite, span_lint_and_sugg};
2 use rustc_errors::Applicability;
3 use rustc_hir as hir;
4 use rustc_lint::LateContext;
5 use rustc_middle::ty;
6 use rustc_span::symbol::sym;
7
8 use super::CLONE_ON_REF_PTR;
9
10 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
11     let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
12
13     if let ty::Adt(_, subst) = obj_ty.kind() {
14         let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
15             "Rc"
16         } else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
17             "Arc"
18         } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
19             "Weak"
20         } else {
21             return;
22         };
23
24         let snippet = snippet_with_macro_callsite(cx, arg.span, "..");
25
26         span_lint_and_sugg(
27             cx,
28             CLONE_ON_REF_PTR,
29             expr.span,
30             "using `.clone()` on a ref-counted pointer",
31             "try this",
32             format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet),
33             Applicability::Unspecified, // Sometimes unnecessary ::<_> after Rc/Arc/Weak
34         );
35     }
36 }