2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2022-01-17 06:29:07 -06:00
|
|
|
use clippy_utils::source::snippet_with_context;
|
|
|
|
use clippy_utils::ty::peel_mid_ty_refs;
|
2021-04-22 04:31:13 -05:00
|
|
|
use clippy_utils::{is_diag_item_method, is_diag_trait_item};
|
2021-03-12 08:30:50 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2022-01-17 06:29:07 -06:00
|
|
|
use rustc_span::sym;
|
2021-03-12 08:30:50 -06:00
|
|
|
|
|
|
|
use super::IMPLICIT_CLONE;
|
|
|
|
|
2022-01-17 06:29:07 -06:00
|
|
|
pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
|
2021-03-12 08:30:50 -06:00
|
|
|
if_chain! {
|
2021-04-22 04:31:13 -05:00
|
|
|
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
|
2021-12-17 06:40:22 -06:00
|
|
|
if is_clone_like(cx, method_name, method_def_id);
|
2021-04-08 10:50:13 -05:00
|
|
|
let return_type = cx.typeck_results().expr_ty(expr);
|
2022-01-17 06:29:07 -06:00
|
|
|
let input_type = cx.typeck_results().expr_ty(recv);
|
|
|
|
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
|
2022-03-04 14:28:41 -06:00
|
|
|
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
|
2022-01-25 01:42:52 -06:00
|
|
|
if return_type == input_type;
|
2021-03-12 08:30:50 -06:00
|
|
|
then {
|
2022-01-17 06:29:07 -06:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
|
2021-03-12 08:30:50 -06:00
|
|
|
span_lint_and_sugg(
|
2021-04-22 04:31:13 -05:00
|
|
|
cx,
|
|
|
|
IMPLICIT_CLONE,
|
2022-01-17 06:29:07 -06:00
|
|
|
expr.span,
|
2022-09-23 12:42:59 -05:00
|
|
|
&format!("implicitly cloning a `{ty_name}` by calling `{method_name}` on its dereferenced type"),
|
2021-03-12 08:30:50 -06:00
|
|
|
"consider using",
|
2022-01-17 06:29:07 -06:00
|
|
|
if ref_count > 1 {
|
2022-09-23 12:42:59 -05:00
|
|
|
format!("({}{recv_snip}).clone()", "*".repeat(ref_count - 1))
|
2022-01-17 06:29:07 -06:00
|
|
|
} else {
|
2022-09-23 12:42:59 -05:00
|
|
|
format!("{recv_snip}.clone()")
|
2022-01-17 06:29:07 -06:00
|
|
|
},
|
|
|
|
app,
|
2021-03-12 08:30:50 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 06:40:22 -06:00
|
|
|
|
|
|
|
/// Returns true if the named method can be used to clone the receiver.
|
|
|
|
/// Note that `to_string` is not flagged by `implicit_clone`. So other lints that call
|
|
|
|
/// `is_clone_like` and that do flag `to_string` must handle it separately. See, e.g.,
|
|
|
|
/// `is_to_owned_like` in `unnecessary_to_owned.rs`.
|
|
|
|
pub fn is_clone_like(cx: &LateContext<'_>, method_name: &str, method_def_id: hir::def_id::DefId) -> bool {
|
|
|
|
match method_name {
|
|
|
|
"to_os_string" => is_diag_item_method(cx, method_def_id, sym::OsStr),
|
|
|
|
"to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
|
|
|
|
"to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path),
|
2022-04-07 09:44:37 -05:00
|
|
|
"to_vec" => cx
|
|
|
|
.tcx
|
|
|
|
.impl_of_method(method_def_id)
|
|
|
|
.filter(|&impl_did| cx.tcx.type_of(impl_did).is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none())
|
|
|
|
.is_some(),
|
2021-12-17 06:40:22 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|