Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joshtriplett opened this issue Apr 30, 2024 · 1 comment · May be fixed by #124580
Open

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

joshtriplett opened this issue Apr 30, 2024 · 1 comment · May be fixed by #124580
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

joshtriplett commented Apr 30, 2024

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

@joshtriplett joshtriplett added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 30, 2024
@gurry
Copy link
Contributor

gurry commented Apr 30, 2024

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants