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

Incorrect warning for unused field. #92995

Closed
rcls opened this issue Jan 17, 2022 · 1 comment
Closed

Incorrect warning for unused field. #92995

rcls opened this issue Jan 17, 2022 · 1 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.

Comments

@rcls
Copy link

rcls commented Jan 17, 2022

(Note entirely sure what the intended behaviour is here, but named fields versus tuple fields are treated differently, which seems wrong.)

Given the following code: (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8c1b3d36eae323990f50c01d182c042d)

#[derive(Debug)]
pub struct Foo{x:i32}

fn main() {
    let foo = Foo{x:1};
    println!("{:?}", foo);
}

The current output is:

warning: field is never read: `x`

But the field is actually read due to the #[derive(Default)] and the use of that.

Arguable, a warning here is sensible in that as the usage is "debug" output only. But in that case it is hard to see why this doesn't warn also (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a03f26d9f9c53bda612313913bfaf67e):

#[derive(Debug)]
pub struct Foo(i32);

fn main() {
    let foo = Foo(1);
    println!("{:?}", foo);
}

At least these two should be treated consistently?

@rcls rcls 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 Jan 17, 2022
@ehuss
Copy link
Contributor

ehuss commented Jan 17, 2022

Thanks for the report! This is currently intended behavior, as otherwise adding #[derive(Debug)] would prevent the unused warning from working. I understand it can be problematic as actually using the debug formatting seems like it should count as a "use". There is a deeper explanation at #88900 (comment).

Closing as a duplicate of #88900, and the tuple issue is #92790.

@ehuss ehuss closed this as completed Jan 17, 2022
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

No branches or pull requests

2 participants