Skip to content

Do not propose to auto-derive Clone in presence of unsafe fields #14559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
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
4 changes: 4 additions & 0 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &h
{
return;
}
// The presence of `unsafe` fields prevents deriving `Clone` automatically
if ty_adt.all_fields().any(|f| f.safety.is_unsafe()) {
return;
}

span_lint_and_note(
cx,
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
dead_code
)]
#![warn(clippy::expl_impl_clone_on_copy)]
#![expect(incomplete_features)] // `unsafe_fields` is incomplete for the time being
#![feature(unsafe_fields)] // `clone()` cannot be derived automatically on unsafe fields

#[derive(Copy)]
struct Qux;
Expand Down Expand Up @@ -112,4 +114,19 @@ impl<T: Copy> Clone for Packed<T> {
}
}

fn issue14558() {
pub struct Valid {
pub unsafe actual: (),
}

unsafe impl Copy for Valid {}

impl Clone for Valid {
#[inline]
fn clone(&self) -> Self {
*self
}
}
}

fn main() {}
20 changes: 10 additions & 10 deletions tests/ui/derive.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:13:1
--> tests/ui/derive.rs:15:1
|
LL | / impl Clone for Qux {
LL | |
Expand All @@ -10,7 +10,7 @@ LL | | }
| |_^
|
note: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:13:1
--> tests/ui/derive.rs:15:1
|
LL | / impl Clone for Qux {
LL | |
Expand All @@ -23,7 +23,7 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]`

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:39:1
--> tests/ui/derive.rs:41:1
|
LL | / impl<'a> Clone for Lt<'a> {
LL | |
Expand All @@ -34,7 +34,7 @@ LL | | }
| |_^
|
note: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:39:1
--> tests/ui/derive.rs:41:1
|
LL | / impl<'a> Clone for Lt<'a> {
LL | |
Expand All @@ -45,7 +45,7 @@ LL | | }
| |_^

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:52:1
--> tests/ui/derive.rs:54:1
|
LL | / impl Clone for BigArray {
LL | |
Expand All @@ -56,7 +56,7 @@ LL | | }
| |_^
|
note: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:52:1
--> tests/ui/derive.rs:54:1
|
LL | / impl Clone for BigArray {
LL | |
Expand All @@ -67,7 +67,7 @@ LL | | }
| |_^

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:65:1
--> tests/ui/derive.rs:67:1
|
LL | / impl Clone for FnPtr {
LL | |
Expand All @@ -78,7 +78,7 @@ LL | | }
| |_^
|
note: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:65:1
--> tests/ui/derive.rs:67:1
|
LL | / impl Clone for FnPtr {
LL | |
Expand All @@ -89,7 +89,7 @@ LL | | }
| |_^

error: you are implementing `Clone` explicitly on a `Copy` type
--> tests/ui/derive.rs:87:1
--> tests/ui/derive.rs:89:1
|
LL | / impl<T: Clone> Clone for Generic2<T> {
LL | |
Expand All @@ -100,7 +100,7 @@ LL | | }
| |_^
|
note: consider deriving `Clone` or removing `Copy`
--> tests/ui/derive.rs:87:1
--> tests/ui/derive.rs:89:1
|
LL | / impl<T: Clone> Clone for Generic2<T> {
LL | |
Expand Down