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-12-17 13:40:22 +0100
committerflip1995 <philipp.krones@embecosm.com>2021-12-17 13:40:22 +0100
commitece0946d7f60bdee8b9544753a3f1a3acddcb1f7 (patch)
tree78cfdf1e08b149bae2122b496dd4cfd6fb55c4d4 /clippy_lints/src/methods/implicit_clone.rs
parentb2f8a27ff2662b440d0dd65cdb609d303b2fa280 (diff)
downloadrust-ece0946d7f60bdee8b9544753a3f1a3acddcb1f7.tar.gz
rust-ece0946d7f60bdee8b9544753a3f1a3acddcb1f7.zip
Merge commit '23d11428de3e973b34a5090a78d62887f821c90e' into clippyup
Diffstat (limited to 'clippy_lints/src/methods/implicit_clone.rs')
-rw-r--r--clippy_lints/src/methods/implicit_clone.rs29
1 files changed, 20 insertions, 9 deletions
diff --git a/clippy_lints/src/methods/implicit_clone.rs b/clippy_lints/src/methods/implicit_clone.rs
index 81c42de145f..90492ffda3c 100644
--- a/clippy_lints/src/methods/implicit_clone.rs
+++ b/clippy_lints/src/methods/implicit_clone.rs
@@ -12,15 +12,7 @@ use super::IMPLICIT_CLONE;
 pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, span: Span) {
     if_chain! {
         if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
-        if 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),
-            "to_vec" => cx.tcx.impl_of_method(method_def_id)
-                .map(|impl_did| Some(impl_did) == cx.tcx.lang_items().slice_alloc_impl())
-                == Some(true),
-            _ => false,
-        };
+        if is_clone_like(cx, method_name, method_def_id);
         let return_type = cx.typeck_results().expr_ty(expr);
         let input_type = cx.typeck_results().expr_ty(recv).peel_refs();
         if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
@@ -38,3 +30,22 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
         }
     }
 }
+
+/// 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),
+        "to_vec" => {
+            cx.tcx
+                .impl_of_method(method_def_id)
+                .map(|impl_did| Some(impl_did) == cx.tcx.lang_items().slice_alloc_impl())
+                == Some(true)
+        },
+        _ => false,
+    }
+}