]> git.lizzy.rs Git - rust.git/commitdiff
Moves `clone_on_ref_ptr` to be a restriction lint
authorAdam Lusch <alusch@gmail.com>
Mon, 15 Jan 2018 03:58:09 +0000 (19:58 -0800)
committerAdam Lusch <alusch@gmail.com>
Mon, 15 Jan 2018 03:58:09 +0000 (19:58 -0800)
Also updates the suggestion to include the full type (e.g. `Arc<Foo>::clone(&rc)`)
and adds a case using trait objects to the UI tests.

clippy_lints/src/methods.rs
tests/ui/unnecessary_clone.rs

index 64335f81a6347110eeb9c6901a6f037eb99b28c9..8803c4e3019b67668c1ca23c98378fe551b41f32 100644 (file)
 /// ```rust
 /// x.clone()
 /// ```
-declare_lint! {
+declare_restriction_lint! {
     pub CLONE_ON_REF_PTR,
-    Warn,
     "using 'clone' on a ref-counted pointer"
 }
 
@@ -1013,24 +1012,26 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, arg_t
 fn lint_clone_on_ref_ptr(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr) {
     let (obj_ty, _) = walk_ptrs_ty_depth(cx.tables.expr_ty(arg));
 
-    let caller_type = if match_type(cx, obj_ty, &paths::RC) {
-        "Rc"
-    } else if match_type(cx, obj_ty, &paths::ARC) {
-        "Arc"
-    } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
-        "Weak"
-    } else {
-        return;
-    };
+    if let ty::TyAdt(_, subst) = obj_ty.sty {
+        let caller_type = if match_type(cx, obj_ty, &paths::RC) {
+            "Rc"
+        } else if match_type(cx, obj_ty, &paths::ARC) {
+            "Arc"
+        } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
+            "Weak"
+        } else {
+            return;
+        };
 
-    span_lint_and_sugg(
-        cx,
-        CLONE_ON_REF_PTR,
-        expr.span,
-        "using '.clone()' on a ref-counted pointer",
-        "try this",
-        format!("{}::clone(&{})", caller_type, snippet(cx, arg.span, "_")),
-    );
+        span_lint_and_sugg(
+            cx,
+            CLONE_ON_REF_PTR,
+            expr.span,
+            "using '.clone()' on a ref-counted pointer",
+            "try this",
+            format!("{}<{}>::clone(&{})", caller_type, subst.type_at(0), snippet(cx, arg.span, "_")),
+        );
+    }
 }
 
 
index f33def9eb4ef65811e2bb97082cbf6986ea39382..96166ed4f139aa5c2e518bbbaa13f983d4772cdf 100644 (file)
@@ -1,3 +1,4 @@
+#![warn(clone_on_ref_ptr)]
 #![allow(unused)]
 
 use std::collections::HashSet;
@@ -5,6 +6,10 @@
 use std::rc::{self, Rc};
 use std::sync::{self, Arc};
 
+trait SomeTrait {}
+struct SomeImpl;
+impl SomeTrait for SomeImpl {}
+
 fn main() {}
 
 fn clone_on_copy() {
@@ -34,7 +39,8 @@ fn clone_on_ref_ptr() {
     arc_weak.clone();
     sync::Weak::clone(&arc_weak);
 
-
+    let x = Arc::new(SomeImpl);
+    let _: Arc<SomeTrait> = x.clone(); 
 }
 
 fn clone_on_copy_generic<T: Copy>(t: T) {