Skip to content

Commit

Permalink
Moves clone_on_ref_ptr to be a restriction lint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alusch committed Jan 15, 2018
1 parent 41a710e commit 1615813
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
39 changes: 20 additions & 19 deletions clippy_lints/src/methods.rs
Expand Up @@ -361,9 +361,8 @@ declare_lint! {
/// ```rust
/// x.clone()
/// ```
declare_lint! {
declare_restriction_lint! {
pub CLONE_ON_REF_PTR,
Warn,
"using 'clone' on a ref-counted pointer"
}

Expand Down Expand Up @@ -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, "_")),
);
}
}


Expand Down
8 changes: 7 additions & 1 deletion tests/ui/unnecessary_clone.rs
@@ -1,10 +1,15 @@
#![warn(clone_on_ref_ptr)]
#![allow(unused)]

use std::collections::HashSet;
use std::collections::VecDeque;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

trait SomeTrait {}
struct SomeImpl;
impl SomeTrait for SomeImpl {}

fn main() {}

fn clone_on_copy() {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1615813

Please sign in to comment.