]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/implicit_clone.rs
Auto merge of #105436 - nnethercote:inline-place_contents_drop_state_cannot_differ...
[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::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         then {
23             let mut app = Applicability::MachineApplicable;
24             let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
25             span_lint_and_sugg(
26                 cx,
27                 IMPLICIT_CLONE,
28                 expr.span,
29                 &format!("implicitly cloning a `{ty_name}` by calling `{method_name}` on its dereferenced type"),
30                 "consider using",
31                 if ref_count > 1 {
32                     format!("({}{recv_snip}).clone()", "*".repeat(ref_count - 1))
33                 } else {
34                     format!("{recv_snip}.clone()")
35                 },
36                 app,
37             );
38         }
39     }
40 }
41
42 /// Returns true if the named method can be used to clone the receiver.
43 /// Note that `to_string` is not flagged by `implicit_clone`. So other lints that call
44 /// `is_clone_like` and that do flag `to_string` must handle it separately. See, e.g.,
45 /// `is_to_owned_like` in `unnecessary_to_owned.rs`.
46 pub fn is_clone_like(cx: &LateContext<'_>, method_name: &str, method_def_id: hir::def_id::DefId) -> bool {
47     match method_name {
48         "to_os_string" => is_diag_item_method(cx, method_def_id, sym::OsStr),
49         "to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
50         "to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path),
51         "to_vec" => cx
52             .tcx
53             .impl_of_method(method_def_id)
54             .filter(|&impl_did| cx.tcx.type_of(impl_did).is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none())
55             .is_some(),
56         _ => false,
57     }
58 }