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;
7 use rustc_lint::LateContext;
9 use rustc_span::symbol::{sym, Symbol};
11 use super::CLONE_ON_REF_PTR;
13 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
14 if !(args.len() == 1 && method_name == sym::clone) {
18 let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
20 if let ty::Adt(_, subst) = obj_ty.kind() {
21 let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
23 } else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
25 } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
31 let snippet = snippet_with_macro_callsite(cx, arg.span, "..");
37 "using `.clone()` on a ref-counted pointer",
39 format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet),
40 Applicability::Unspecified, // Sometimes unnecessary ::<_> after Rc/Arc/Weak