rust-lang / rust-lang/rust-clippy
`unneeded_struct_pattern` triggers when matching 3rd-party crate's variant with `{ .. }`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Turning a unit variant into a struct or tuple variant is a major breaking change.
Sometimes, downstream code that consumes an upstream crate's enum variants might care only about "which variant was chosen" and doesn't care whether the variant might (now or in the future) have associated data. In such a situation, I feel it isn't unreasonable to use MyEnum::MyVariant { .. } syntax regardless of whether MyVariant is a unit or not.
Of course, MyEnum::MyVariant is sufficient syntax if the variant is a unit. But in this case, we don't "own" the MyEnum type, and changes in the variant kind aren't up to us. Adding the { .. } is explicit about wishing to be future-proof to changes in the variant kind, so I think it shouldn't trigger a lint — especially not an on-by-default lint.
Lint Name
unneeded_struct_pattern
Reproducer
I tried this code:
fn example(kind: &cargo_metadata::TargetKind) {
match kind {
cargo_metadata::TargetKind::Lib { .. } => todo!(),
_ => todo!(),
}
}
I saw this happen:
warning: struct pattern is not needed for a unit variant
--> src/lib.rs:12:40
|
12 | cargo_metadata::TargetKind::Lib { .. } => todo!(),
| ^^^^^^^ help: remove the struct pattern
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unneeded_struct_pattern
= note: `#[warn(clippy::unneeded_struct_pattern)]` on by default
I expected to see this happen:
No lint reported. My code is saying "we don't care if this 3rd-party crate's variant becomes a tuple or struct variant in the future" and attempting to remain resilient to a potential future breaking change.
Version
rustc 1.86.0-nightly (bef3c3b01 2025-02-04)
binary: rustc
commit-hash: bef3c3b01f690de16738b1c9f36470fbfc6ac623
commit-date: 2025-02-04
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7
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 with the unneeded_struct_pattern lint and reproduce the report using the shown cargo_metadata::TargetKind::Lib { .. } match on the stated Rust nightly version. Check the lint's existing behavior for unit variants and confirm that the third-party variant pattern produces no warning while the intended lint cases remain covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100