Skip to content

Commit a14eab3

Browse files
committed
Auto merge of #5745 - montrivo:copy_on_clone, r=phansch
clone_on_copy - add machine applicability Fix #4826. Change the applicability of the lint clone_on_copy. Split a test file and run rustfix on the clone_on_copy part. changelog: clone_on_copy - add machine applicability
2 parents 583d644 + 6bf5434 commit a14eab3

File tree

6 files changed

+129
-70
lines changed

6 files changed

+129
-70
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
20412041
}
20422042
span_lint_and_then(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type", |diag| {
20432043
if let Some((text, snip)) = snip {
2044-
diag.span_suggestion(expr.span, text, snip, Applicability::Unspecified);
2044+
diag.span_suggestion(expr.span, text, snip, Applicability::MachineApplicable);
20452045
}
20462046
});
20472047
}

tests/ui/clone_on_copy.fixed

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run-rustfix
2+
3+
#![allow(
4+
unused,
5+
clippy::redundant_clone,
6+
clippy::deref_addrof,
7+
clippy::no_effect,
8+
clippy::unnecessary_operation
9+
)]
10+
11+
use std::cell::RefCell;
12+
use std::rc::{self, Rc};
13+
use std::sync::{self, Arc};
14+
15+
fn main() {}
16+
17+
fn is_ascii(ch: char) -> bool {
18+
ch.is_ascii()
19+
}
20+
21+
fn clone_on_copy() {
22+
42;
23+
24+
vec![1].clone(); // ok, not a Copy type
25+
Some(vec![1]).clone(); // ok, not a Copy type
26+
*(&42);
27+
28+
let rc = RefCell::new(0);
29+
*rc.borrow();
30+
31+
// Issue #4348
32+
let mut x = 43;
33+
let _ = &x.clone(); // ok, getting a ref
34+
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
35+
is_ascii('z');
36+
37+
// Issue #5436
38+
let mut vec = Vec::new();
39+
vec.push(42);
40+
}

tests/ui/clone_on_copy.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run-rustfix
2+
3+
#![allow(
4+
unused,
5+
clippy::redundant_clone,
6+
clippy::deref_addrof,
7+
clippy::no_effect,
8+
clippy::unnecessary_operation
9+
)]
10+
11+
use std::cell::RefCell;
12+
use std::rc::{self, Rc};
13+
use std::sync::{self, Arc};
14+
15+
fn main() {}
16+
17+
fn is_ascii(ch: char) -> bool {
18+
ch.is_ascii()
19+
}
20+
21+
fn clone_on_copy() {
22+
42.clone();
23+
24+
vec![1].clone(); // ok, not a Copy type
25+
Some(vec![1]).clone(); // ok, not a Copy type
26+
(&42).clone();
27+
28+
let rc = RefCell::new(0);
29+
rc.borrow().clone();
30+
31+
// Issue #4348
32+
let mut x = 43;
33+
let _ = &x.clone(); // ok, getting a ref
34+
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
35+
is_ascii('z'.clone());
36+
37+
// Issue #5436
38+
let mut vec = Vec::new();
39+
vec.push(42.clone());
40+
}

tests/ui/clone_on_copy.stderr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: using `clone` on a `Copy` type
2+
--> $DIR/clone_on_copy.rs:22:5
3+
|
4+
LL | 42.clone();
5+
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
6+
|
7+
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
8+
9+
error: using `clone` on a `Copy` type
10+
--> $DIR/clone_on_copy.rs:26:5
11+
|
12+
LL | (&42).clone();
13+
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`
14+
15+
error: using `clone` on a `Copy` type
16+
--> $DIR/clone_on_copy.rs:29:5
17+
|
18+
LL | rc.borrow().clone();
19+
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`
20+
21+
error: using `clone` on a `Copy` type
22+
--> $DIR/clone_on_copy.rs:35:14
23+
|
24+
LL | is_ascii('z'.clone());
25+
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`
26+
27+
error: using `clone` on a `Copy` type
28+
--> $DIR/clone_on_copy.rs:39:14
29+
|
30+
LL | vec.push(42.clone());
31+
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
32+
33+
error: aborting due to 5 previous errors
34+

tests/ui/unnecessary_clone.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,6 @@ impl SomeTrait for SomeImpl {}
1313

1414
fn main() {}
1515

16-
fn is_ascii(ch: char) -> bool {
17-
ch.is_ascii()
18-
}
19-
20-
fn clone_on_copy() {
21-
42.clone();
22-
23-
vec![1].clone(); // ok, not a Copy type
24-
Some(vec![1]).clone(); // ok, not a Copy type
25-
(&42).clone();
26-
27-
let rc = RefCell::new(0);
28-
rc.borrow().clone();
29-
30-
// Issue #4348
31-
let mut x = 43;
32-
let _ = &x.clone(); // ok, getting a ref
33-
'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
34-
is_ascii('z'.clone());
35-
36-
// Issue #5436
37-
let mut vec = Vec::new();
38-
vec.push(42.clone());
39-
}
40-
4116
fn clone_on_ref_ptr() {
4217
let rc = Rc::new(true);
4318
let arc = Arc::new(true);

tests/ui/unnecessary_clone.stderr

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,51 @@
1-
error: using `clone` on a `Copy` type
2-
--> $DIR/unnecessary_clone.rs:21:5
3-
|
4-
LL | 42.clone();
5-
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
6-
|
7-
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
8-
9-
error: using `clone` on a `Copy` type
10-
--> $DIR/unnecessary_clone.rs:25:5
11-
|
12-
LL | (&42).clone();
13-
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`
14-
15-
error: using `clone` on a `Copy` type
16-
--> $DIR/unnecessary_clone.rs:28:5
17-
|
18-
LL | rc.borrow().clone();
19-
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`
20-
21-
error: using `clone` on a `Copy` type
22-
--> $DIR/unnecessary_clone.rs:34:14
23-
|
24-
LL | is_ascii('z'.clone());
25-
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`
26-
27-
error: using `clone` on a `Copy` type
28-
--> $DIR/unnecessary_clone.rs:38:14
29-
|
30-
LL | vec.push(42.clone());
31-
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
32-
331
error: using `.clone()` on a ref-counted pointer
34-
--> $DIR/unnecessary_clone.rs:48:5
2+
--> $DIR/unnecessary_clone.rs:23:5
353
|
364
LL | rc.clone();
375
| ^^^^^^^^^^ help: try this: `Rc::<bool>::clone(&rc)`
386
|
397
= note: `-D clippy::clone-on-ref-ptr` implied by `-D warnings`
408

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

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

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

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

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

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

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

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

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

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

129-
error: aborting due to 16 previous errors
99+
error: aborting due to 11 previous errors
130100

0 commit comments

Comments
 (0)