]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/implicit_clone.rs
Rollup merge of #105792 - Ezrashaw:add-e0320-long-docs, r=GuillaumeGomez
[rust.git] / src / tools / clippy / clippy_lints / src / methods / implicit_clone.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_context;
3 use clippy_utils::ty::{implements_trait, peel_mid_ty_refs};
4 use clippy_utils::{is_diag_item_method, is_diag_trait_item};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_lint::LateContext;
9 use rustc_span::sym;
10
11 use super::IMPLICIT_CLONE;
12
13 pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
14     if_chain! {
15         if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
16         if is_clone_like(cx, method_name, method_def_id);
17         let return_type = cx.typeck_results().expr_ty(expr);
18         let input_type = cx.typeck_results().expr_ty(recv);
19         let (input_type, ref_count) = peel_mid_ty_refs(input_type);
20         if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
21         if return_type == input_type;
22         if let Some(clone_trait) = cx.tcx.lang_items().clone_trait();
23         if implements_trait(cx, return_type, clone_trait, &[]);
24         then {
25             let mut app = Applicability::MachineApplicable;
26             let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
27             span_lint_and_sugg(
28                 cx,
29                 IMPLICIT_CLONE,
30                 expr.span,
31                 &format!("implicitly cloning a `{ty_name}` by calling `{method_name}` on its dereferenced type"),
32                 "consider using",
33                 if ref_count > 1 {
34                     format!("({}{recv_snip}).clone()", "*".repeat(ref_count - 1))
35                 } else {
36                     format!("{recv_snip}.clone()")
37                 },
38                 app,
39             );
40         }
41     }
42 }
43
44 /// Returns true if the named method can be used to clone the receiver.
45 /// Note that `to_string` is not flagged by `implicit_clone`. So other lints that call
46 /// `is_clone_like` and that do flag `to_string` must handle it separately. See, e.g.,
47 /// `is_to_owned_like` in `unnecessary_to_owned.rs`.
48 pub fn is_clone_like(cx: &LateContext<'_>, method_name: &str, method_def_id: hir::def_id::DefId) -> bool {
49     match method_name {
50         "to_os_string" => is_diag_item_method(cx, method_def_id, sym::OsStr),
51         "to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
52         "to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path),
53         "to_vec" => cx
54             .tcx
55             .impl_of_method(method_def_id)
56             .filter(|&impl_did| cx.tcx.type_of(impl_did).is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none())
57             .is_some(),
58         _ => false,
59     }
60 }