Skip to content

Add clippy lint: use_destructuring#16807

Open
emilk wants to merge 6 commits intorust-lang:masterfrom
emilk:emilk/lint/use_destructuring
Open

Add clippy lint: use_destructuring#16807
emilk wants to merge 6 commits intorust-lang:masterfrom
emilk:emilk/lint/use_destructuring

Conversation

@emilk
Copy link
Copy Markdown

@emilk emilk commented Apr 4, 2026

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
Copy link
Copy Markdown

github-actions bot commented Apr 4, 2026

Lintcheck changes for 12e7dab

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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 4, 2026

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
Copy link
Copy Markdown
Member

dswij commented Apr 4, 2026

r? clippy

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

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

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.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants