Skip to content

Commit 8dfcaa0

Browse files
authored
Do not propose to auto-derive Clone in presence of unsafe fields (rust-lang#14559)
`unsafe_fields` is an incomplete feature; comments have been put near `#![expect(incomplete_features)]` to ensure that we revisit the situation when the feature becomes complete. changelog: [`expl_impl_clone_on_copy`]: do not lint in the presence of `unsafe` fields Fixes rust-lang#14558
2 parents 3f6025a + 315ea95 commit 8dfcaa0

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

clippy_lints/src/derive.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &h
349349
{
350350
return;
351351
}
352+
// The presence of `unsafe` fields prevents deriving `Clone` automatically
353+
if ty_adt.all_fields().any(|f| f.safety.is_unsafe()) {
354+
return;
355+
}
352356

353357
span_lint_and_note(
354358
cx,

tests/ui/derive.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
dead_code
77
)]
88
#![warn(clippy::expl_impl_clone_on_copy)]
9+
#![expect(incomplete_features)] // `unsafe_fields` is incomplete for the time being
10+
#![feature(unsafe_fields)] // `clone()` cannot be derived automatically on unsafe fields
911

1012
#[derive(Copy)]
1113
struct Qux;
@@ -112,4 +114,19 @@ impl<T: Copy> Clone for Packed<T> {
112114
}
113115
}
114116

117+
fn issue14558() {
118+
pub struct Valid {
119+
pub unsafe actual: (),
120+
}
121+
122+
unsafe impl Copy for Valid {}
123+
124+
impl Clone for Valid {
125+
#[inline]
126+
fn clone(&self) -> Self {
127+
*self
128+
}
129+
}
130+
}
131+
115132
fn main() {}

tests/ui/derive.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you are implementing `Clone` explicitly on a `Copy` type
2-
--> tests/ui/derive.rs:13:1
2+
--> tests/ui/derive.rs:15:1
33
|
44
LL | / impl Clone for Qux {
55
LL | |
@@ -10,7 +10,7 @@ LL | | }
1010
| |_^
1111
|
1212
note: consider deriving `Clone` or removing `Copy`
13-
--> tests/ui/derive.rs:13:1
13+
--> tests/ui/derive.rs:15:1
1414
|
1515
LL | / impl Clone for Qux {
1616
LL | |
@@ -23,7 +23,7 @@ LL | | }
2323
= help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]`
2424

2525
error: you are implementing `Clone` explicitly on a `Copy` type
26-
--> tests/ui/derive.rs:39:1
26+
--> tests/ui/derive.rs:41:1
2727
|
2828
LL | / impl<'a> Clone for Lt<'a> {
2929
LL | |
@@ -34,7 +34,7 @@ LL | | }
3434
| |_^
3535
|
3636
note: consider deriving `Clone` or removing `Copy`
37-
--> tests/ui/derive.rs:39:1
37+
--> tests/ui/derive.rs:41:1
3838
|
3939
LL | / impl<'a> Clone for Lt<'a> {
4040
LL | |
@@ -45,7 +45,7 @@ LL | | }
4545
| |_^
4646

4747
error: you are implementing `Clone` explicitly on a `Copy` type
48-
--> tests/ui/derive.rs:52:1
48+
--> tests/ui/derive.rs:54:1
4949
|
5050
LL | / impl Clone for BigArray {
5151
LL | |
@@ -56,7 +56,7 @@ LL | | }
5656
| |_^
5757
|
5858
note: consider deriving `Clone` or removing `Copy`
59-
--> tests/ui/derive.rs:52:1
59+
--> tests/ui/derive.rs:54:1
6060
|
6161
LL | / impl Clone for BigArray {
6262
LL | |
@@ -67,7 +67,7 @@ LL | | }
6767
| |_^
6868

6969
error: you are implementing `Clone` explicitly on a `Copy` type
70-
--> tests/ui/derive.rs:65:1
70+
--> tests/ui/derive.rs:67:1
7171
|
7272
LL | / impl Clone for FnPtr {
7373
LL | |
@@ -78,7 +78,7 @@ LL | | }
7878
| |_^
7979
|
8080
note: consider deriving `Clone` or removing `Copy`
81-
--> tests/ui/derive.rs:65:1
81+
--> tests/ui/derive.rs:67:1
8282
|
8383
LL | / impl Clone for FnPtr {
8484
LL | |
@@ -89,7 +89,7 @@ LL | | }
8989
| |_^
9090

9191
error: you are implementing `Clone` explicitly on a `Copy` type
92-
--> tests/ui/derive.rs:87:1
92+
--> tests/ui/derive.rs:89:1
9393
|
9494
LL | / impl<T: Clone> Clone for Generic2<T> {
9595
LL | |
@@ -100,7 +100,7 @@ LL | | }
100100
| |_^
101101
|
102102
note: consider deriving `Clone` or removing `Copy`
103-
--> tests/ui/derive.rs:87:1
103+
--> tests/ui/derive.rs:89:1
104104
|
105105
LL | / impl<T: Clone> Clone for Generic2<T> {
106106
LL | |

0 commit comments

Comments
 (0)