Skip to content

Commit 1df2e38

Browse files
committed
Auto merge of #6443 - matthiaskrgr:clone_on_copy_type, r=ebroto
clone_on_copy: show the type in the lint message changelog: clone_on_copy: show the type in the lint message
2 parents 684f17e + b2cb6ff commit 1df2e38

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,11 +2177,17 @@ fn lint_clone_on_copy(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Exp
21772177
} else {
21782178
snip = None;
21792179
}
2180-
span_lint_and_then(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type", |diag| {
2181-
if let Some((text, snip)) = snip {
2182-
diag.span_suggestion(expr.span, text, snip, Applicability::MachineApplicable);
2183-
}
2184-
});
2180+
span_lint_and_then(
2181+
cx,
2182+
CLONE_ON_COPY,
2183+
expr.span,
2184+
&format!("using `clone` on type `{}` which implements the `Copy` trait", ty),
2185+
|diag| {
2186+
if let Some((text, snip)) = snip {
2187+
diag.span_suggestion(expr.span, text, snip, Applicability::MachineApplicable);
2188+
}
2189+
},
2190+
);
21852191
}
21862192
}
21872193

tests/ui/clone_on_copy.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
error: using `clone` on a `Copy` type
1+
error: using `clone` on type `i32` which implements the `Copy` trait
22
--> $DIR/clone_on_copy.rs:22: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

9-
error: using `clone` on a `Copy` type
9+
error: using `clone` on type `i32` which implements the `Copy` trait
1010
--> $DIR/clone_on_copy.rs:26:5
1111
|
1212
LL | (&42).clone();
1313
| ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)`
1414

15-
error: using `clone` on a `Copy` type
15+
error: using `clone` on type `i32` which implements the `Copy` trait
1616
--> $DIR/clone_on_copy.rs:29:5
1717
|
1818
LL | rc.borrow().clone();
1919
| ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()`
2020

21-
error: using `clone` on a `Copy` type
21+
error: using `clone` on type `char` which implements the `Copy` trait
2222
--> $DIR/clone_on_copy.rs:35:14
2323
|
2424
LL | is_ascii('z'.clone());
2525
| ^^^^^^^^^^^ help: try removing the `clone` call: `'z'`
2626

27-
error: using `clone` on a `Copy` type
27+
error: using `clone` on type `i32` which implements the `Copy` trait
2828
--> $DIR/clone_on_copy.rs:39:14
2929
|
3030
LL | vec.push(42.clone());

tests/ui/unnecessary_clone.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ error: using `.clone()` on a ref-counted pointer
3030
LL | let _: Arc<dyn SomeTrait> = x.clone();
3131
| ^^^^^^^^^ help: try this: `Arc::<SomeImpl>::clone(&x)`
3232

33-
error: using `clone` on a `Copy` type
33+
error: using `clone` on type `T` which implements the `Copy` trait
3434
--> $DIR/unnecessary_clone.rs:40:5
3535
|
3636
LL | t.clone();
3737
| ^^^^^^^^^ help: try removing the `clone` call: `t`
3838
|
3939
= note: `-D clippy::clone-on-copy` implied by `-D warnings`
4040

41-
error: using `clone` on a `Copy` type
41+
error: using `clone` on type `std::option::Option<T>` which implements the `Copy` trait
4242
--> $DIR/unnecessary_clone.rs:42:5
4343
|
4444
LL | Some(t).clone();
@@ -60,7 +60,7 @@ help: or try being explicit if you are sure, that you want to clone a reference
6060
LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6262

63-
error: using `clone` on a `Copy` type
63+
error: using `clone` on type `many_derefs::E` which implements the `Copy` trait
6464
--> $DIR/unnecessary_clone.rs:84:20
6565
|
6666
LL | let _: E = a.clone();

0 commit comments

Comments
 (0)