Skip to content

dead_code suggestion for unused fields does not account for changes to auto trait implementations #119645

Open

Description

I tried this code:

use std::rc::Rc;

struct Pending(Rc<()>);

fn pending() -> Pending {
    Pending(Rc::new(()))
}

fn main() {
    let _ = pending();
}

(The original code: https://github.com/rust-lang/futures-rs/blob/72e7e397cdfe7574e9b5d8845cafefc6e5dda70a/futures-executor/tests/local_pool.rs#L13)

I expected to see this happen: no warning or a warning suggests Pending(PhantomData<Rc<()>>).

Instead, this happened:

A warning suggests Pending(()).

error: field `0` is never read
  --> futures-executor/tests/local_pool.rs:13:16
   |
13 | struct Pending(Rc<()>);
   |        ------- ^^^^^^
   |        |
   |        field in this struct
   |
   = note: `-D dead-code` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(dead_code)]`
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
   |
13 | struct Pending(());
   |                ~~

Changing Rc<()> to () changes auto trait implementations of Pending, so dead_code should not suggest such a code or should suggest both code (PhantomData<Rc<()>> and ()) with explanations.

Also, if it suggests PhantomData, it should also be noted that PhantomData is not able to propagate all auto-traits (e.g., Unpin). (although this is not a problem in the above case) EDIT: see #119645 (comment)

https://github.com/rust-lang/futures-rs/actions/runs/7428509435/job/20215931270

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (595bc6f00 2024-01-05)
binary: rustc
commit-hash: 595bc6f00369475047538fdae1ff8cea692ac385
commit-date: 2024-01-05
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

This has been shown in the latest nightly (nightly-2024-01-06) since #118297 (cc @shepmaster). IMO, it should not be changed to be warned by default with such a lint with a wrong suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.C-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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