]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
5099ac24 | 2 | use clippy_utils::source::snippet_with_context; |
9c376795 | 3 | use clippy_utils::ty::{implements_trait, peel_mid_ty_refs}; |
cdc7bbd5 | 4 | use clippy_utils::{is_diag_item_method, is_diag_trait_item}; |
f20569fa XL |
5 | use if_chain::if_chain; |
6 | use rustc_errors::Applicability; | |
7 | use rustc_hir as hir; | |
f20569fa | 8 | use rustc_lint::LateContext; |
5099ac24 | 9 | use rustc_span::sym; |
f20569fa XL |
10 | |
11 | use super::IMPLICIT_CLONE; | |
f20569fa | 12 | |
5099ac24 | 13 | pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) { |
f20569fa | 14 | if_chain! { |
cdc7bbd5 | 15 | if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); |
a2a8927a | 16 | if is_clone_like(cx, method_name, method_def_id); |
cdc7bbd5 | 17 | let return_type = cx.typeck_results().expr_ty(expr); |
5099ac24 FG |
18 | let input_type = cx.typeck_results().expr_ty(recv); |
19 | let (input_type, ref_count) = peel_mid_ty_refs(input_type); | |
5e7ed085 | 20 | if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did())); |
5099ac24 | 21 | if return_type == input_type; |
9c376795 FG |
22 | if let Some(clone_trait) = cx.tcx.lang_items().clone_trait(); |
23 | if implements_trait(cx, return_type, clone_trait, &[]); | |
f20569fa | 24 | then { |
5099ac24 FG |
25 | let mut app = Applicability::MachineApplicable; |
26 | let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0; | |
f20569fa | 27 | span_lint_and_sugg( |
cdc7bbd5 XL |
28 | cx, |
29 | IMPLICIT_CLONE, | |
5099ac24 | 30 | expr.span, |
2b03887a | 31 | &format!("implicitly cloning a `{ty_name}` by calling `{method_name}` on its dereferenced type"), |
f20569fa | 32 | "consider using", |
5099ac24 | 33 | if ref_count > 1 { |
2b03887a | 34 | format!("({}{recv_snip}).clone()", "*".repeat(ref_count - 1)) |
5099ac24 | 35 | } else { |
2b03887a | 36 | format!("{recv_snip}.clone()") |
5099ac24 FG |
37 | }, |
38 | app, | |
f20569fa XL |
39 | ); |
40 | } | |
41 | } | |
42 | } | |
a2a8927a XL |
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), | |
04454e1e FG |
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(), | |
a2a8927a XL |
58 | _ => false, |
59 | } | |
60 | } |