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

"field is never read" warning despite Debug being used explicitly in the code #123562

Closed
fjarri opened this issue Apr 6, 2024 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fjarri
Copy link

fjarri commented Apr 6, 2024

use core::fmt;

#[derive(Debug)]
pub struct Error(String);

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        // warning
        write!(f, "{:?}", self)

        // no warning
        // write!(f, "{}", self.0)
    }
}

pub fn api_call(x: usize) -> Result<usize, Error> {
    if x == 0 {
        Err(Error("Error".into()))
    }
    else {
        Ok(x)
    }
}

Let's start from the case where impl Display is commented out. clippy gives a dead code warning about the unused String field in Error (see the discussion in #88900).

The suggested workaround is an explicit Display impl. But if I add that, using the Debug internally to generate the displayed string, the warning is still there:

warning: field `0` is never read
 --> src/lib.rs:4:18
  |
4 | pub struct Error(String);
  |            ----- ^^^^^^
  |            |
  |            field in this struct
  |
  = 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
  |
4 | pub struct Error(());
  |                  ~~

In order to get rid of the warning, I must explicitly use the .0 field in the Display impl. While it is alright for this simple case, for error types which are enums with a lot of fields, this becomes very tiresome. Is it the intended behavior?

Meta

rustc 1.77.1 (7cf61eb 2024-03-27)
binary: rustc
commit-hash: 7cf61eb
commit-date: 2024-03-27
host: aarch64-apple-darwin
release: 1.77.1
LLVM version: 17.0.6
(seems to have been introduced in 1.77.0)

@fjarri fjarri added the C-bug Category: This is a bug. label Apr 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 6, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Apr 6, 2024

Looks like duplicate of #123068

@fjarri
Copy link
Author

fjarri commented Apr 6, 2024

Yes, sorry, didn't find it when searching. Commented there.

@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. 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. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 6, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Apr 6, 2024

Closing this as a duplicate of #123068.

@jieyouxu jieyouxu closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2024
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. C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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