Skip to content

warning of dead code against live enum field #134168

Closed as duplicate of#88900
Closed as duplicate of#88900
@hkBst

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

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