rust-lang / rust-lang/rust-clippy

Lint tuple structs that could be named-field structs

Open
#1,926 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-style T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

As pointed out in rust-lang/rust#42998 it's an anti-pattern in Rust to create a tuple struct and then used named accessors, as in:

/// Type for Unicode Version.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct UnicodeVersion(
    pub u16, // Major version
    pub u16, // Minor version
    pub u16 // Micro (or Update) version
);

impl UnicodeVersion {
    /// Major version
    pub fn major(&self) -> u16 {
        self.0
    }

    /// Minor version
    pub fn minor(&self) -> u16 {
        self.1
    }

    /// Micro (or Update) version
    pub fn micro(&self) -> u16 {
        self.2
    }
}

We should lint cases like this and suggest replacing the tuple struct with a named-field struct, and adding a new method instead.

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 from the tuple-struct example in this issue and determine how Clippy should identify accessor methods that only expose positional fields. Done means the lint reports such cases, suggests a named-field struct and a new method, and handles the illustrated Rust pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.