Skip to content

Commit 57913b4

Browse files
authored
Honor allow/expect attributes on ADT and impl Clone nodes (#15849)
changelog: [`expl_impl_clone_on_copy`]: honor `allow`/`expect` attributes on both the type declaration and the `impl` Fixes #15842 r? @blyxyas
2 parents 77ce8b8 + 69bd890 commit 57913b4

File tree

3 files changed

+28
-57
lines changed

3 files changed

+28
-57
lines changed

clippy_lints/src/derive/expl_impl_clone_on_copy.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint_hir_and_then;
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::fulfill_or_allowed;
23
use clippy_utils::ty::{implements_trait, is_copy};
34
use rustc_hir::{self as hir, HirId, Item};
45
use rustc_lint::LateContext;
@@ -60,14 +61,16 @@ pub(super) fn check<'tcx>(
6061
return;
6162
}
6263

63-
span_lint_hir_and_then(
64+
if fulfill_or_allowed(cx, EXPL_IMPL_CLONE_ON_COPY, [adt_hir_id]) {
65+
return;
66+
}
67+
68+
span_lint_and_help(
6469
cx,
6570
EXPL_IMPL_CLONE_ON_COPY,
66-
adt_hir_id,
6771
item.span,
6872
"you are implementing `Clone` explicitly on a `Copy` type",
69-
|diag| {
70-
diag.span_help(item.span, "consider deriving `Clone` or removing `Copy`");
71-
},
73+
None,
74+
"consider deriving `Clone` or removing `Copy`",
7275
);
7376
}

tests/ui/derive.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn issue14558() {
132132
fn main() {}
133133

134134
mod issue15708 {
135-
// Check that the lint posts on the type definition node
135+
// Check that `allow`/`expect` attributes are recognized on the type definition node
136136
#[expect(clippy::expl_impl_clone_on_copy)]
137137
#[derive(Copy)]
138138
struct S;
@@ -143,3 +143,16 @@ mod issue15708 {
143143
}
144144
}
145145
}
146+
147+
mod issue15842 {
148+
#[derive(Copy)]
149+
struct S;
150+
151+
// Check that `allow`/`expect` attributes are recognized on the `impl Clone` node
152+
#[expect(clippy::expl_impl_clone_on_copy)]
153+
impl Clone for S {
154+
fn clone(&self) -> Self {
155+
S
156+
}
157+
}
158+
}

tests/ui/derive.stderr

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@ LL | | fn clone(&self) -> Self {
99
LL | | }
1010
| |_^
1111
|
12-
help: consider deriving `Clone` or removing `Copy`
13-
--> tests/ui/derive.rs:15:1
14-
|
15-
LL | / impl Clone for Qux {
16-
LL | |
17-
LL | |
18-
LL | | fn clone(&self) -> Self {
19-
... |
20-
LL | | }
21-
| |_^
12+
= help: consider deriving `Clone` or removing `Copy`
2213
= note: `-D clippy::expl-impl-clone-on-copy` implied by `-D warnings`
2314
= help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]`
2415

@@ -33,16 +24,7 @@ LL | | fn clone(&self) -> Self {
3324
LL | | }
3425
| |_^
3526
|
36-
help: consider deriving `Clone` or removing `Copy`
37-
--> tests/ui/derive.rs:41:1
38-
|
39-
LL | / impl<'a> Clone for Lt<'a> {
40-
LL | |
41-
LL | |
42-
LL | | fn clone(&self) -> Self {
43-
... |
44-
LL | | }
45-
| |_^
27+
= help: consider deriving `Clone` or removing `Copy`
4628

4729
error: you are implementing `Clone` explicitly on a `Copy` type
4830
--> tests/ui/derive.rs:54:1
@@ -55,16 +37,7 @@ LL | | fn clone(&self) -> Self {
5537
LL | | }
5638
| |_^
5739
|
58-
help: consider deriving `Clone` or removing `Copy`
59-
--> tests/ui/derive.rs:54:1
60-
|
61-
LL | / impl Clone for BigArray {
62-
LL | |
63-
LL | |
64-
LL | | fn clone(&self) -> Self {
65-
... |
66-
LL | | }
67-
| |_^
40+
= help: consider deriving `Clone` or removing `Copy`
6841

6942
error: you are implementing `Clone` explicitly on a `Copy` type
7043
--> tests/ui/derive.rs:67:1
@@ -77,16 +50,7 @@ LL | | fn clone(&self) -> Self {
7750
LL | | }
7851
| |_^
7952
|
80-
help: consider deriving `Clone` or removing `Copy`
81-
--> tests/ui/derive.rs:67:1
82-
|
83-
LL | / impl Clone for FnPtr {
84-
LL | |
85-
LL | |
86-
LL | | fn clone(&self) -> Self {
87-
... |
88-
LL | | }
89-
| |_^
53+
= help: consider deriving `Clone` or removing `Copy`
9054

9155
error: you are implementing `Clone` explicitly on a `Copy` type
9256
--> tests/ui/derive.rs:89:1
@@ -99,16 +63,7 @@ LL | | fn clone(&self) -> Self {
9963
LL | | }
10064
| |_^
10165
|
102-
help: consider deriving `Clone` or removing `Copy`
103-
--> tests/ui/derive.rs:89:1
104-
|
105-
LL | / impl<T: Clone> Clone for Generic2<T> {
106-
LL | |
107-
LL | |
108-
LL | | fn clone(&self) -> Self {
109-
... |
110-
LL | | }
111-
| |_^
66+
= help: consider deriving `Clone` or removing `Copy`
11267

11368
error: aborting due to 5 previous errors
11469

0 commit comments

Comments
 (0)