]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/implicit_clone.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / implicit_clone.rs
CommitLineData
f20569fa
XL
1use crate::utils::span_lint_and_sugg;
2use if_chain::if_chain;
3use rustc_errors::Applicability;
4use rustc_hir as hir;
5use rustc_hir::ExprKind;
6use rustc_lint::LateContext;
7use rustc_middle::ty::TyS;
8use rustc_span::symbol::Symbol;
9
10use super::IMPLICIT_CLONE;
11use clippy_utils::is_diagnostic_assoc_item;
12
13pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, trait_diagnostic: Symbol) {
14 if_chain! {
15 if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind;
16 let return_type = cx.typeck_results().expr_ty(&expr);
17 let input_type = cx.typeck_results().expr_ty(arg).peel_refs();
18 if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
19 if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
20 if TyS::same_type(return_type, input_type);
21 if is_diagnostic_assoc_item(cx, expr_def_id, trait_diagnostic);
22 then {
23 span_lint_and_sugg(
24 cx,IMPLICIT_CLONE,method_path.ident.span,
25 &format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_path.ident.name),
26 "consider using",
27 "clone".to_string(),
28 Applicability::MachineApplicable
29 );
30 }
31 }
32}