rust-lang / rust-lang/rust-clippy

Can a struct have too many fields?

Open
#16,103 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.