rust-lang / rust-lang/rust-clippy
Lint when `serde(flatten)` causes a name collision or "shadowed" field
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
When using [serde(flatten)] with derive(serde::Deserialize), if there's a name collision between fields on the "outer" struct and the flattened struct, then, during deserialization, only the field on the "outer" struct is set.
Playground example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=046c25618923ffdb3194ce7cadaecb0f
Advantage
Avoid accidentally using default values in deserialization
Drawbacks
The obvious one is that this is, as far as I can tell, overly specific, applying only to serde(flatten).
Possibly serde_derive itself could be augmented to make this a hard error.
Example
struct Outer {
#[serde(flatten)]
inner: Inner,
data: String,
}
struct Inner {
data: String
...
}
Could be written as:
struct Outer {
#[serde(flatten)]
inner: Inner,
}
// < Inner would not change >
or
struct Outer {
#[serde(flatten)]
inner: Inner,
data: String,
}
struct Inner {
...
}
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
The issue names no repository file or test. Start by reproducing the linked Rust Playground example with serde(flatten) and derive(serde::Deserialize), then inspect how Clippy lints are structured. Done means a lint detects the outer and flattened-field name collision and avoids false positives for the alternatives described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100