about summary refs log tree commit diff
path: root/clippy_lints/src/methods/implicit_clone.rs
diff options
context:
space:
mode:
authorflip1995 <philipp.krones@embecosm.com>2021-03-12 15:30:50 +0100
committerflip1995 <philipp.krones@embecosm.com>2021-03-12 15:30:50 +0100
commitf2f2a005b4efd3e44ac6a02ea2b9660d28401679 (patch)
treec4ece65dffee2aa79eaa3b7f190765a95055f815 /clippy_lints/src/methods/implicit_clone.rs
parent36a27ecaacad74f69b21a12bc66b826f11f2d44e (diff)
downloadrust-f2f2a005b4efd3e44ac6a02ea2b9660d28401679.tar.gz
rust-f2f2a005b4efd3e44ac6a02ea2b9660d28401679.zip
Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup
Diffstat (limited to 'clippy_lints/src/methods/implicit_clone.rs')
-rw-r--r--clippy_lints/src/methods/implicit_clone.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/implicit_clone.rs b/clippy_lints/src/methods/implicit_clone.rs
new file mode 100644
index 00000000000..a769493d11d
--- /dev/null
+++ b/clippy_lints/src/methods/implicit_clone.rs
@@ -0,0 +1,32 @@
+use crate::utils::span_lint_and_sugg;
+use if_chain::if_chain;
+use rustc_errors::Applicability;
+use rustc_hir as hir;
+use rustc_hir::ExprKind;
+use rustc_lint::LateContext;
+use rustc_middle::ty::TyS;
+use rustc_span::symbol::Symbol;
+
+use super::IMPLICIT_CLONE;
+use clippy_utils::is_diagnostic_assoc_item;
+
+pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, trait_diagnostic: Symbol) {
+    if_chain! {
+        if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind;
+        let return_type = cx.typeck_results().expr_ty(&expr);
+        let input_type = cx.typeck_results().expr_ty(arg).peel_refs();
+        if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
+        if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
+        if TyS::same_type(return_type, input_type);
+        if is_diagnostic_assoc_item(cx, expr_def_id, trait_diagnostic);
+        then {
+            span_lint_and_sugg(
+                cx,IMPLICIT_CLONE,method_path.ident.span,
+                &format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_path.ident.name),
+                "consider using",
+                "clone".to_string(),
+                Applicability::MachineApplicable
+            );
+        }
+    }
+}