rust-lang / rust-lang/rust-clippy
Can a struct have too many fields?
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
Currently clippy::too_many_arguments raises a warning, but there's no lint that warns about a struct or an enum variant having too many fields. That means that if a struct has 3 or 4 fields that a particular function needs, and they're otherwise unrelated, I have an incentive to pass in the entire struct instead. This encourages stamp coupling over data coupling, and makes borrow conflicts more likely, and makes missing (such as BTreeMap::get_mut_many()) and caveatful (such as HashMap::get_mut_many()'s runtime check for distinct keys) methods to work around these conflicts more painful.
Is there some opinionated reason to favor passing in the entire struct in such cases, or was it just an oversight?
Advantage
Encourages devs not to borrow an entire struct in a function that only needs a few fields of that struct, and makes it possible to call that function even while other fields in the struct are mutably borrowed.
Drawbacks
May hurt readability.
Example
<code>
Could be written as:
<code>
Comparison with existing lints
No response
Additional Context
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 reviewing the existing clippy::too_many_arguments lint, which the issue cites as the closest comparison. Define the intended behavior and threshold for structs and enum variants, then establish representative examples and tests that show when the new lint should and should not trigger.
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
- Needs clarification
- Newbie friendliness
- 25/100