Skip to content

Commit 5e8c0c5

Browse files
committed
Auto merge of rust-lang#5441 - rabisg0:fix/clone-on-copy, r=phansch
Check for clone-on-copy in argument positions Earlier if arguments to method calls matched the above pattern they were not reported. This patch ensures such arguments are checked as well. Fixes rust-lang#5436 changelog: apply clone_on_copy lint to func args as well
2 parents 0353f21 + 183c4ab commit 5e8c0c5

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

clippy_lints/src/methods/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1947,9 +1947,10 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
19471947
match &cx.tcx.hir().get(parent) {
19481948
hir::Node::Expr(parent) => match parent.kind {
19491949
// &*x is a nop, &x.clone() is not
1950-
hir::ExprKind::AddrOf(..) |
1950+
hir::ExprKind::AddrOf(..) => return,
19511951
// (*x).func() is useless, x.clone().func() can work in case func borrows mutably
1952-
hir::ExprKind::MethodCall(..) => return,
1952+
hir::ExprKind::MethodCall(_, _, parent_args) if expr.hir_id == parent_args[0].hir_id => return,
1953+
19531954
_ => {},
19541955
},
19551956
hir::Node::Stmt(stmt) => {

tests/ui/unnecessary_clone.rs

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ impl SomeTrait for SomeImpl {}
1313

1414
fn main() {}
1515

16+
fn is_ascii(ch: char) -> bool {
17+
ch.is_ascii()
18+
}
19+
1620
fn clone_on_copy() {
1721
42.clone();
1822

@@ -27,6 +31,11 @@ fn clone_on_copy() {
2731
let mut x = 43;
2832
let _ = &x.clone(); // ok, getting a ref
2933
'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());
3039
}
3140

3241
fn clone_on_ref_ptr() {

tests/ui/unnecessary_clone.stderr

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,81 @@
11
error: using `clone` on a `Copy` type
2-
--> $DIR/unnecessary_clone.rs:17:5
2+
--> $DIR/unnecessary_clone.rs:21:5
33
|
44
LL | 42.clone();
55
| ^^^^^^^^^^ help: try removing the `clone` call: `42`
66
|
77
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
88

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

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

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+
2133
error: using `.clone()` on a ref-counted pointer
22-
--> $DIR/unnecessary_clone.rs:39:5
34+
--> $DIR/unnecessary_clone.rs:48:5
2335
|
2436
LL | rc.clone();
2537
| ^^^^^^^^^^ help: try this: `Rc::<bool>::clone(&rc)`
2638
|
2739
= note: `-D clippy::clone-on-ref-ptr` implied by `-D warnings`
2840

2941
error: using `.clone()` on a ref-counted pointer
30-
--> $DIR/unnecessary_clone.rs:42:5
42+
--> $DIR/unnecessary_clone.rs:51:5
3143
|
3244
LL | arc.clone();
3345
| ^^^^^^^^^^^ help: try this: `Arc::<bool>::clone(&arc)`
3446

3547
error: using `.clone()` on a ref-counted pointer
36-
--> $DIR/unnecessary_clone.rs:45:5
48+
--> $DIR/unnecessary_clone.rs:54:5
3749
|
3850
LL | rcweak.clone();
3951
| ^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&rcweak)`
4052

4153
error: using `.clone()` on a ref-counted pointer
42-
--> $DIR/unnecessary_clone.rs:48:5
54+
--> $DIR/unnecessary_clone.rs:57:5
4355
|
4456
LL | arc_weak.clone();
4557
| ^^^^^^^^^^^^^^^^ help: try this: `Weak::<bool>::clone(&arc_weak)`
4658

4759
error: using `.clone()` on a ref-counted pointer
48-
--> $DIR/unnecessary_clone.rs:52:33
60+
--> $DIR/unnecessary_clone.rs:61:33
4961
|
5062
LL | let _: Arc<dyn SomeTrait> = x.clone();
5163
| ^^^^^^^^^ help: try this: `Arc::<SomeImpl>::clone(&x)`
5264

5365
error: using `clone` on a `Copy` type
54-
--> $DIR/unnecessary_clone.rs:56:5
66+
--> $DIR/unnecessary_clone.rs:65:5
5567
|
5668
LL | t.clone();
5769
| ^^^^^^^^^ help: try removing the `clone` call: `t`
5870

5971
error: using `clone` on a `Copy` type
60-
--> $DIR/unnecessary_clone.rs:58:5
72+
--> $DIR/unnecessary_clone.rs:67:5
6173
|
6274
LL | Some(t).clone();
6375
| ^^^^^^^^^^^^^^^ help: try removing the `clone` call: `Some(t)`
6476

6577
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
66-
--> $DIR/unnecessary_clone.rs:64:22
78+
--> $DIR/unnecessary_clone.rs:73:22
6779
|
6880
LL | let z: &Vec<_> = y.clone();
6981
| ^^^^^^^^^
@@ -79,10 +91,10 @@ LL | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8092

8193
error: using `clone` on a `Copy` type
82-
--> $DIR/unnecessary_clone.rs:100:20
94+
--> $DIR/unnecessary_clone.rs:109:20
8395
|
8496
LL | let _: E = a.clone();
8597
| ^^^^^^^^^ help: try dereferencing it: `*****a`
8698

87-
error: aborting due to 12 previous errors
99+
error: aborting due to 14 previous errors
88100

0 commit comments

Comments
 (0)