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

warning of dead code against live enum field #134168

Closed
hkBst opened this issue Dec 11, 2024 · 1 comment
Closed

warning of dead code against live enum field #134168

hkBst opened this issue Dec 11, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hkBst
Copy link
Contributor

hkBst commented Dec 11, 2024

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

@hkBst hkBst 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 Dec 11, 2024
@clubby789 clubby789 added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 11, 2024
@jieyouxu jieyouxu added the L-dead_code Lint: dead_code label Dec 11, 2024
@jieyouxu
Copy link
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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dead_code Lint: dead_code 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

3 participants