Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions clippy_lints/src/derive/expl_impl_clone_on_copy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::fulfill_or_allowed;
use clippy_utils::ty::{implements_trait, is_copy};
use rustc_hir::{self as hir, HirId, Item};
use rustc_lint::LateContext;
Expand Down Expand Up @@ -60,14 +61,16 @@ pub(super) fn check<'tcx>(
return;
}

span_lint_hir_and_then(
if fulfill_or_allowed(cx, EXPL_IMPL_CLONE_ON_COPY, [adt_hir_id]) {
return;
}

span_lint_and_help(
cx,
EXPL_IMPL_CLONE_ON_COPY,
adt_hir_id,
item.span,
"you are implementing `Clone` explicitly on a `Copy` type",
|diag| {
diag.span_help(item.span, "consider deriving `Clone` or removing `Copy`");
},
None,
"consider deriving `Clone` or removing `Copy`",
);
}
15 changes: 14 additions & 1 deletion tests/ui/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn issue14558() {
fn main() {}

mod issue15708 {
// Check that the lint posts on the type definition node
// Check that `allow`/`expect` attributes are recognized on the type definition node
#[expect(clippy::expl_impl_clone_on_copy)]
#[derive(Copy)]
struct S;
Expand All @@ -143,3 +143,16 @@ mod issue15708 {
}
}
}

mod issue15842 {
#[derive(Copy)]
struct S;

// Check that `allow`/`expect` attributes are recognized on the `impl Clone` node
#[expect(clippy::expl_impl_clone_on_copy)]
impl Clone for S {
fn clone(&self) -> Self {
S
}
}
}
55 changes: 5 additions & 50 deletions tests/ui/derive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ LL | | fn clone(&self) -> Self {
LL | | }
| |_^
|
help: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:15:1
|
LL | / impl Clone for Qux {
LL | |
LL | |
LL | | fn clone(&self) -> Self {
... |
LL | | }
| |_^
= help: consider deriving `Clone` or removing `Copy`
= note: `-D clippy::expl-impl-clone-on-copy` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]`

Expand All @@ -33,16 +24,7 @@ LL | | fn clone(&self) -> Self {
LL | | }
| |_^
|
help: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:41:1
|
LL | / impl<'a> Clone for Lt<'a> {
LL | |
LL | |
LL | | fn clone(&self) -> Self {
... |
LL | | }
| |_^
= help: consider deriving `Clone` or removing `Copy`

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:54:1
Expand All @@ -55,16 +37,7 @@ LL | | fn clone(&self) -> Self {
LL | | }
| |_^
|
help: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:54:1
|
LL | / impl Clone for BigArray {
LL | |
LL | |
LL | | fn clone(&self) -> Self {
... |
LL | | }
| |_^
= help: consider deriving `Clone` or removing `Copy`

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:67:1
Expand All @@ -77,16 +50,7 @@ LL | | fn clone(&self) -> Self {
LL | | }
| |_^
|
help: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:67:1
|
LL | / impl Clone for FnPtr {
LL | |
LL | |
LL | | fn clone(&self) -> Self {
... |
LL | | }
| |_^
= help: consider deriving `Clone` or removing `Copy`

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:89:1
Expand All @@ -99,16 +63,7 @@ LL | | fn clone(&self) -> Self {
LL | | }
| |_^
|
help: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:89:1
|
LL | / impl<T: Clone> Clone for Generic2<T> {
LL | |
LL | |
LL | | fn clone(&self) -> Self {
... |
LL | | }
| |_^
= help: consider deriving `Clone` or removing `Copy`

error: aborting due to 5 previous errors

Loading