rust-lang / rust-lang/rust-clippy
`unsafe_derive_deserialize` triggers on "transparent" types
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
The raison d'être of this lint is "Deriving serde::Deserialize will create a constructor that may violate invariants held by another constructor." however, if a type is an enum, or a struct with all public fields, then there is already a constructor (or multiple) so there can't be any invariants.
// This struct may have an invariant.
// For example: `bar` is non-negative.
struct Foo {
bar: i32
}
// However, this struct cannot have any invariants
// since anyone can write: Foo { bar: /* any value */ }
struct Foo {
pub bar: i32
}
Lint Name
unsafe_derive_deserialize
Reproducer
I tried this code:
#![deny(clippy::unsafe_derive_deserialize)]
#![allow(dead_code)]
extern crate serde; // 1.0.228
#[derive(serde::Deserialize)]
struct Foo {
pub bar: i32,
}
impl Foo {
unsafe fn oof() {}
}
I saw this happen:
error: you are deriving `serde::Deserialize` on a type that has methods using `unsafe`
--> src/lib.rs:6:10
|
6 | #[derive(serde::Deserialize)]
| ^^^^^^^^^^^^^^^^^^
|
= help: consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#unsafe_derive_deserialize
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::unsafe_derive_deserialize)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the derive macro `serde::Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
I expected to see this happen:
nothing
Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the unsafe_derive_deserialize diagnostic with the Rust and serde example in the issue. Trace the lint's handling of transparent types, including structs with all public fields and enums. Done means the lint no longer reports the shown public-field struct while preserving its intended behavior for types that may hold invariants.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100