Skip to content

Commit

Permalink
copy_on_clone - add machine applicability
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Jun 23, 2020
1 parent 7427065 commit 6bf5434
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 70 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
}
span_lint_and_then(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type", |diag| {
if let Some((text, snip)) = snip {
diag.span_suggestion(expr.span, text, snip, Applicability::Unspecified);
diag.span_suggestion(expr.span, text, snip, Applicability::MachineApplicable);
}
});
}
Expand Down
40 changes: 40 additions & 0 deletions tests/ui/clone_on_copy.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// run-rustfix

#![allow(
unused,
clippy::redundant_clone,
clippy::deref_addrof,
clippy::no_effect,
clippy::unnecessary_operation
)]

use std::cell::RefCell;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

fn main() {}

fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

fn clone_on_copy() {
42;

vec![1].clone(); // ok, not a Copy type
Some(vec![1]).clone(); // ok, not a Copy type
*(&42);

let rc = RefCell::new(0);
*rc.borrow();

// Issue #4348
let mut x = 43;
let _ = &x.clone(); // ok, getting a ref
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
is_ascii('z');

// Issue #5436
let mut vec = Vec::new();
vec.push(42);
}
40 changes: 40 additions & 0 deletions tests/ui/clone_on_copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// run-rustfix

#![allow(
unused,
clippy::redundant_clone,
clippy::deref_addrof,
clippy::no_effect,
clippy::unnecessary_operation
)]

use std::cell::RefCell;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

fn main() {}

fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

fn clone_on_copy() {
42.clone();

vec![1].clone(); // ok, not a Copy type
Some(vec![1]).clone(); // ok, not a Copy type
(&42).clone();

let rc = RefCell::new(0);
rc.borrow().clone();

// Issue #4348
let mut x = 43;
let _ = &x.clone(); // ok, getting a ref
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
is_ascii('z'.clone());

// Issue #5436
let mut vec = Vec::new();
vec.push(42.clone());
}
34 changes: 34 additions & 0 deletions tests/ui/clone_on_copy.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
error: using `clone` on a `Copy` type
--> $DIR/clone_on_copy.rs:22:5
|
LL | 42.clone();
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
= note: `-D clippy::clone-on-copy` implied by `-D warnings`

error: using `clone` on a `Copy` type
--> $DIR/clone_on_copy.rs:26:5
|
LL | (&42).clone();
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`

error: using `clone` on a `Copy` type
--> $DIR/clone_on_copy.rs:29:5
|
LL | rc.borrow().clone();
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`

error: using `clone` on a `Copy` type
--> $DIR/clone_on_copy.rs:35:14
|
LL | is_ascii('z'.clone());
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`

error: using `clone` on a `Copy` type
--> $DIR/clone_on_copy.rs:39:14
|
LL | vec.push(42.clone());
| ^^^^^^^^^^ help: try removing the `clone` call: `42`

error: aborting due to 5 previous errors

25 changes: 0 additions & 25 deletions tests/ui/unnecessary_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@ impl SomeTrait for SomeImpl {}

fn main() {}

fn is_ascii(ch: char) -> bool {
ch.is_ascii()
}

fn clone_on_copy() {
42.clone();

vec![1].clone(); // ok, not a Copy type
Some(vec![1]).clone(); // ok, not a Copy type
(&42).clone();

let rc = RefCell::new(0);
rc.borrow().clone();

// Issue #4348
let mut x = 43;
let _ = &x.clone(); // ok, getting a ref
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
is_ascii('z'.clone());

// Issue #5436
let mut vec = Vec::new();
vec.push(42.clone());
}

fn clone_on_ref_ptr() {
let rc = Rc::new(true);
let arc = Arc::new(true);
Expand Down
58 changes: 14 additions & 44 deletions tests/ui/unnecessary_clone.stderr
Original file line number Diff line number Diff line change
@@ -1,81 +1,51 @@
error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:21:5
|
LL | 42.clone();
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
|
= note: `-D clippy::clone-on-copy` implied by `-D warnings`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:25:5
|
LL | (&42).clone();
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:28:5
|
LL | rc.borrow().clone();
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:34:14
|
LL | is_ascii('z'.clone());
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:38:14
|
LL | vec.push(42.clone());
| ^^^^^^^^^^ help: try removing the `clone` call: `42`

error: using `.clone()` on a ref-counted pointer
--> $DIR/unnecessary_clone.rs:48:5
--> $DIR/unnecessary_clone.rs:23:5
|
LL | rc.clone();
| ^^^^^^^^^^ help: try this: `Rc::<bool>::clone(&rc)`
|
= note: `-D clippy::clone-on-ref-ptr` implied by `-D warnings`

error: using `.clone()` on a ref-counted pointer
--> $DIR/unnecessary_clone.rs:51:5
--> $DIR/unnecessary_clone.rs:26:5
|
LL | arc.clone();
| ^^^^^^^^^^^ help: try this: `Arc::<bool>::clone(&arc)`

error: using `.clone()` on a ref-counted pointer
--> $DIR/unnecessary_clone.rs:54:5
--> $DIR/unnecessary_clone.rs:29:5
|
LL | rcweak.clone();
| ^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&rcweak)`

error: using `.clone()` on a ref-counted pointer
--> $DIR/unnecessary_clone.rs:57:5
--> $DIR/unnecessary_clone.rs:32:5
|
LL | arc_weak.clone();
| ^^^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&arc_weak)`

error: using `.clone()` on a ref-counted pointer
--> $DIR/unnecessary_clone.rs:61:33
--> $DIR/unnecessary_clone.rs:36:33
|
LL | let _: Arc<dyn SomeTrait> = x.clone();
| ^^^^^^^^^ help: try this: `Arc::<SomeImpl>::clone(&x)`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:65:5
--> $DIR/unnecessary_clone.rs:40:5
|
LL | t.clone();
| ^^^^^^^^^ help: try removing the `clone` call: `t`
|
= note: `-D clippy::clone-on-copy` implied by `-D warnings`

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:67:5
--> $DIR/unnecessary_clone.rs:42:5
|
LL | Some(t).clone();
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`

error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> $DIR/unnecessary_clone.rs:73:22
--> $DIR/unnecessary_clone.rs:48:22
|
LL | let z: &Vec<_> = y.clone();
| ^^^^^^^^^
Expand All @@ -91,13 +61,13 @@ LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:109:20
--> $DIR/unnecessary_clone.rs:84:20
|
LL | let _: E = a.clone();
| ^^^^^^^^^ help: try dereferencing it: `*****a`

error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> $DIR/unnecessary_clone.rs:114:22
--> $DIR/unnecessary_clone.rs:89:22
|
LL | let _ = &mut encoded.clone();
| ^^^^^^^^^^^^^^^
Expand All @@ -112,7 +82,7 @@ LL | let _ = &mut <&[u8]>::clone(encoded);
| ^^^^^^^^^^^^^^^^^^^^^^^

error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> $DIR/unnecessary_clone.rs:115:18
--> $DIR/unnecessary_clone.rs:90:18
|
LL | let _ = &encoded.clone();
| ^^^^^^^^^^^^^^^
Expand All @@ -126,5 +96,5 @@ help: or try being explicit if you are sure, that you want to clone a reference
LL | let _ = &<&[u8]>::clone(encoded);
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 16 previous errors
error: aborting due to 11 previous errors

0 comments on commit 6bf5434

Please sign in to comment.