1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2 use clippy_utils::get_parent_node;
3 use clippy_utils::source::snippet_with_context;
4 use clippy_utils::sugg;
5 use clippy_utils::ty::is_copy;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BindingAnnotation, Expr, ExprKind, MatchSource, Node, PatKind};
8 use rustc_lint::LateContext;
9 use rustc_middle::ty::{self, adjustment::Adjust};
10 use rustc_span::symbol::{sym, Symbol};
12 use super::CLONE_DOUBLE_REF;
13 use super::CLONE_ON_COPY;
15 /// Checks for the `CLONE_ON_COPY` lint.
16 #[allow(clippy::too_many_lines)]
17 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol, args: &[Expr<'_>]) {
18 let arg = match args {
19 [arg] if method_name == sym::clone => arg,
24 .type_dependent_def_id(expr.hir_id)
25 .and_then(|id| cx.tcx.trait_of_item(id))
26 .zip(cx.tcx.lang_items().clone_trait())
27 .map_or(true, |(x, y)| x != y)
31 let arg_adjustments = cx.typeck_results().expr_adjustments(arg);
32 let arg_ty = arg_adjustments
34 .map_or_else(|| cx.typeck_results().expr_ty(arg), |a| a.target);
36 let ty = cx.typeck_results().expr_ty(expr);
37 if let ty::Ref(_, inner, _) = arg_ty.kind() {
38 if let ty::Ref(_, innermost, _) = inner.kind() {
44 "using `clone` on a double-reference; \
45 this will copy the reference of type `{}` instead of cloning the inner type",
49 if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
50 let mut ty = innermost;
52 while let ty::Ref(_, inner, _) = ty.kind() {
56 let refs = "&".repeat(n + 1);
57 let derefs = "*".repeat(n);
58 let explicit = format!("<{}{}>::clone({})", refs, ty, snip);
61 "try dereferencing it",
62 format!("{}({}{}).clone()", refs, derefs, snip.deref()),
63 Applicability::MaybeIncorrect,
67 "or try being explicit if you are sure, that you want to clone a reference",
69 Applicability::MaybeIncorrect,
74 return; // don't report clone_on_copy
79 let parent_is_suffix_expr = match get_parent_node(cx.tcx, expr.hir_id) {
80 Some(Node::Expr(parent)) => match parent.kind {
81 // &*x is a nop, &x.clone() is not
82 ExprKind::AddrOf(..) => return,
83 // (*x).func() is useless, x.clone().func() can work in case func borrows self
84 ExprKind::MethodCall(_, _, [self_arg, ..], _)
85 if expr.hir_id == self_arg.hir_id && ty != cx.typeck_results().expr_ty_adjusted(expr) =>
89 ExprKind::MethodCall(_, _, [self_arg, ..], _) if expr.hir_id == self_arg.hir_id => true,
90 ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
92 | ExprKind::Index(..) => true,
95 // local binding capturing a reference
99 PatKind::Binding(BindingAnnotation::Ref | BindingAnnotation::RefMut, ..)
107 let mut app = Applicability::MachineApplicable;
108 let snip = snippet_with_context(cx, arg.span, expr.span.ctxt(), "_", &mut app).0;
110 let deref_count = arg_adjustments
112 .take_while(|adj| matches!(adj.kind, Adjust::Deref(_)))
114 let (help, sugg) = if deref_count == 0 {
115 ("try removing the `clone` call", snip.into())
116 } else if parent_is_suffix_expr {
117 ("try dereferencing it", format!("({}{})", "*".repeat(deref_count), snip))
119 ("try dereferencing it", format!("{}{}", "*".repeat(deref_count), snip))
126 &format!("using `clone` on type `{}` which implements the `Copy` trait", ty),