Skip to content

unused tuple field warning should suggest deleting field if it's the last field #124556

Closed
@joshtriplett

Description

@joshtriplett

Code

struct T1(u32, u32);
struct T2(u32);

impl T1 {
    fn f(&self) {
        println!("{}", self.0);
    }
}

fn main() {
    let t1 = T1(42, 42);
    let t2 = T2(42);

    t1.f();
}

Current output

warning: field `1` is never read
 --> src/main.rs:2:16
  |
2 | struct T1(u32, u32);
  |        --      ^^^
  |        |
  |        field in this struct
  |
  = note: `T1` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
  |
2 | struct T1(u32, ());
  |                ~~

warning: field `0` is never read
 --> src/main.rs:5:11
  |
5 | struct T2(u32);
  |        -- ^^^
  |        |
  |        field in this struct
  |
  = note: `T2` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
  |
5 | struct T2(());
  |           ~~

Desired output

warning: field `1` is never read
 --> src/main.rs:2:16
  |
2 | struct T1(u32, u32);
  |        --      ^^^
  |        |
  |        field in this struct
  |
  = note: `T1` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default
help: consider removing the field
  |
2 | struct T1(u32);

warning: field `0` is never read
 --> src/main.rs:5:11
  |
5 | struct T2(u32);
  |        -- ^^^
  |        |
  |        field in this struct
  |
  = note: `T2` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
help: consider removing the field
  |
5 | struct T2();

Rationale and extra context

The idea of changing a field to unit type to preserve field numbering makes sense for fields in the middle of a tuple. However, if the unused field is at the end, or it's the only field, then deleting it won't affect field numbering of any other field.

Other cases

No response

Rust Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions