Skip to content

warning of dead code against live enum field #134168

Description

@hkBst

Code

fn main() -> Result<(), Error> {
    let _ = "x".parse::<usize>().map_err(Error::Int)?;
    Ok(())
}

#[derive(Debug)]
enum Error {
    Int(std::num::ParseIntError),
}

Current output

$ cargo r
   Compiling non-dead-code v0.1.0 (/home/marijn/Code/Rust/non-dead-code)
warning: field `0` is never read
 --> src/main.rs:8:9
  |
8 |     Int(std::num::ParseIntError),
  |     --- ^^^^^^^^^^^^^^^^^^^^^^^
  |     |
  |     field in this variant
  |
  = note: `Error` 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
  |
8 |     Int(()),
  |         ~~

warning: `non-dead-code` (bin "non-dead-code") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.43s
     Running `/home/marijn/Code/Rust/non-dead-code/target/debug/non-dead-code`
Error: Int(ParseIntError { kind: InvalidDigit })

Desired output

$ cargo r
   Compiling non-dead-code v0.1.0 (/home/marijn/Code/Rust/non-dead-code)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.43s
     Running `/home/marijn/Code/Rust/non-dead-code/target/debug/non-dead-code`
Error: Int(ParseIntError { kind: InvalidDigit })

Rationale and extra context

I am really confused why the compiler should think that this code is dead, as the output from running it shows that it is live, and replacing the field as suggested leads to less informative output when running.

Other cases

Rust Version

rustc 1.83.0-nightly (90b35a623 2024-11-26) (gentoo)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.5

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 11, 2024
  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Dec 11, 2024
  3. jieyouxu commented on Dec 11, 2024

    @jieyouxu
    Member

    This is a duplicate of #88900, and this is intentional as per Ignore derived Clone and Debug implementations during dead code analysis #85200, which was FCP'd by T-lang in #85200 (comment). See also the issues #88900 and #123418.

    In particular, there is a remark in the diagnostics:

    note: `Error` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    

    This is also identical to #123418.

    Thank you for the report.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-dead_codeLint: dead_codeT-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