Skip to content

Add clippy lint: use_destructuring - #16807

Open
emilk wants to merge 8 commits into
rust-lang:masterfrom
emilk:emilk/lint/use_destructuring
Open

emilk wants to merge 8 commits into
rust-lang:masterfrom
emilk:emilk/lint/use_destructuring

Conversation

@emilk

@emilk emilk commented Apr 4, 2026

Copy link
Copy Markdown

New lint: [use_destructuring]

Adds a pedantic lint that suggests using destructuring when a function accesses all fields of a struct via the self parameter (e.g. self.x, self.y, self.z), and the struct has 3 or more fields. This helps keep code in sync with struct definitions — the compiler will error on the destructuring if a field is added.

Works for both named structs and tuple structs.

Configuration

Two options in clippy.toml:

Option Type Default Description
use-destructuring-min-fields u64 3 Minimum number of struct fields for the lint to trigger.
use-destructuring-scope string "self" Which variables to lint. See below.

use-destructuring-scope values:

Value Description
"self" Only lint the self parameter (default, most conservative).
"Self" Lint any variable whose type matches Self in the enclosing impl block.
"crate" Lint any variable whose type is defined in the current crate.
"*" Lint all struct types, including external ones.

Suppression

The lint does NOT fire when:

  • The struct has fewer fields than the configured minimum (default 3).
  • Not all fields are accessed.
  • The variable is also used as a whole (passed to a function, returned, etc.).
  • Any field is mutated (self.x = 5).
  • The struct is #[non_exhaustive].
  • The struct is a union or enum.
  • The field access is from macro expansion.
  • Destructuring would cause a name collision with an existing local variable.

Example

struct Point { x: f32, y: f32, z: f32 }

impl std::fmt::Display for Point {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Before:
        write!(f, "({}, {}, {})", self.x, self.y, self.z)

        // After:
        // let Self { x, y, z } = self;
        // write!(f, "({x}, {y}, {z})")
    }
}

  • Followed lint naming conventions
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: [use_destructuring]: new pedantic lint suggesting destructuring when all fields of a struct are accessed individually


Implemented with a lot of help from Claude Code

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Apr 4, 2026
@emilk
emilk force-pushed the emilk/lint/use_destructuring branch from 052a460 to 0fae774 Compare April 4, 2026 09:57
Comment thread tests/dogfood.rs Outdated
@github-actions

github-actions Bot commented Apr 4, 2026

Copy link
Copy Markdown

Lintcheck changes for 801a92f

Lint Added Removed Changed
clippy::use_destructuring 522 0 0

This comment will be updated if you push new changes

@emilk
emilk marked this pull request as ready for review April 4, 2026 10:13
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 4, 2026
@rustbot

rustbot commented Apr 4, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@emilk
emilk marked this pull request as draft April 4, 2026 10:43
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 4, 2026
@emilk
emilk force-pushed the emilk/lint/use_destructuring branch 4 times, most recently from 0533a7b to 4c078e7 Compare April 4, 2026 13:50
@emilk
emilk marked this pull request as ready for review April 4, 2026 14:46
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 4, 2026
@dswij

dswij commented Apr 4, 2026

Copy link
Copy Markdown
Member

r? clippy

@rustbot rustbot assigned flip1995 and unassigned dswij Apr 4, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 14, 2026
@emilk
emilk force-pushed the emilk/lint/use_destructuring branch from dc467f9 to 12e7dab Compare April 14, 2026 14:38
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Apr 14, 2026
@rustbot

This comment has been minimized.

@emilk
emilk force-pushed the emilk/lint/use_destructuring branch from 12e7dab to 5eb8f76 Compare September 8, 2026 12:36
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

- `impl_lint_pass` moved from `rustc_session` to `rustc_lint`
- Regenerate `book/src/lint_configuration.md` (`cargo bless --test config-metadata`)
- Dogfood the lint in `needless_range_loop::SpanlessExpr`
Required by the `lint-attributes-organization` test.
@emilk
emilk force-pushed the emilk/lint/use_destructuring branch from 5eb8f76 to 801a92f Compare September 18, 2026 14:38
@rustbot

rustbot commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@CommanderStorm CommanderStorm added llm-assisted For PRs that were partially or completely done with assistance of AI/LLM tools S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. labels Sep 19, 2026
@CommanderStorm

Copy link
Copy Markdown
Contributor

👋🏻 Triage here.

We have recently adopted an new policy regarding LLM use.
Please read https://forge.rust-lang.org/policies/llm-usage.html

Concretely regarding your PR

  • Please disclose clearly which parts of the PR were generated or assisted by Claude and which were written manually.
    A phrase like "lots of help" is too vague for a reviewer to properly evaluate the contribution and if they want to review your PR.
  • The policy strictly prohibits LLMs from writing certain parts of the codebase.
    This includes documentation, soundness-critical code, commit messages, and PR descriptions.
    Please remove any LLM-generated content in these areas and rewrite them manually from scratch.

We hold LLM based contributions to an higher standard than human contributions.
Please help us change this PR to also abide by this policy and make this a suceess 🤗.

@rustbot author
@rustbot label +llm-assisted
r? @ghost

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Sep 19, 2026
@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Error: Failed to set assignee to ghost: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted For PRs that were partially or completely done with assistance of AI/LLM tools needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants