Skip to content

field "never read" warning for field that is used by derive(Debug) #123068

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jkarneges opened this issue Mar 26, 2024 · 8 comments · Fixed by #124460
Closed

field "never read" warning for field that is used by derive(Debug) #123068

jkarneges opened this issue Mar 26, 2024 · 8 comments · Fixed by #124460
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.

Comments

@jkarneges
Copy link
Contributor

jkarneges commented Mar 26, 2024

I tried this code:

use std::str;

#[derive(Debug)]
enum ProcessError {
    Utf8(str::Utf8Error),
    Other,
}

impl From<str::Utf8Error> for ProcessError {
    fn from(e: str::Utf8Error) -> Self {
        Self::Utf8(e)
    }
}

fn process(data: &[u8]) -> Result<(), ProcessError> {
    let s = str::from_utf8(data)?;

    if s != "ok" {
        return Err(ProcessError::Other);
    }

    Ok(())
}

fn main() {
    println!("{:?}", process(&[0xff]));
}

I expected to see this happen: build cleanly

Instead, this happened:

warning: field `0` is never read
 --> src/main.rs:5:10
  |
5 |     Utf8(str::Utf8Error),
  |     ---- ^^^^^^^^^^^^^^
  |     |
  |     field in this variant
  |
  = 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
  |
5 |     Utf8(()),
  |          ~~

Running the program shows the field is used:

Err(Utf8(Utf8Error { valid_up_to: 0, error_len: Some(1) }))

Meta

Rust version 1.77.0 (tested in playground)

@jkarneges jkarneges added the C-bug Category: This is a bug. label Mar 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 26, 2024
@clubby789
Copy link
Contributor

Derived code is intentionally ignored for dead code analysis - if you try just

use std::str;

#[derive(Debug)]
enum ProcessError {
    Utf8(str::Utf8Error),
    Other,
}

You'll get a note explaining this. The note doesn't seem to be attached for never read warnings, just never used

@clubby789 clubby789 added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 26, 2024
@jkarneges
Copy link
Contributor Author

Ah, so it's intentional. Interesting. I assume the solution is to mark it with allow(dead_code) then.

@krtab
Copy link
Contributor

krtab commented Apr 3, 2024

@rustbot claim

@krtab
Copy link
Contributor

krtab commented Apr 5, 2024

@rustbot release-assignment
so @long-long-float can claim it

@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2024

Error: Parsing assign command in comment failed: ...'assignment' | error: expected end of command at >| ' so @long'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@long-long-float
Copy link
Contributor

@rustbot claim

@fjarri
Copy link

fjarri commented Apr 6, 2024

Note that while getting a warning if Debug is just derived but never explicitly used may be understandable (as discussed in other issues), you will get a warning if you do use it explicitly (see #123562) which, in my opinion, should not be the case.

@Noratrieb
Copy link
Member

As mentioned above, the fix for this issue is adding a note about how derived traits are intentionally ignored, not to stop ignoring them.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 24, 2024
…enum-with-debug, r=pnkfelix

Show notice about  "never used" of Debug for enum

Close rust-lang#123068

If an ADT implements `Debug` trait and it is not used, the compiler says a note that indicates intentionally ignored during dead code analysis as [this note](https://github.com/rust-lang/rust/blob/2207179a591f5f252885a94ab014dafeb6e8e9e8/tests/ui/lint/dead-code/unused-variant.stderr#L9).
However this node is not shown for variants that have fields in enum. This PR fixes to show the note.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 24, 2024
…enum-with-debug, r=pnkfelix

Show notice about  "never used" of Debug for enum

Close rust-lang#123068

If an ADT implements `Debug` trait and it is not used, the compiler says a note that indicates intentionally ignored during dead code analysis as [this note](https://github.com/rust-lang/rust/blob/2207179a591f5f252885a94ab014dafeb6e8e9e8/tests/ui/lint/dead-code/unused-variant.stderr#L9).
However this node is not shown for variants that have fields in enum. This PR fixes to show the note.
@bors bors closed this as completed in 00e5f58 Jun 24, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 24, 2024
Rollup merge of rust-lang#124460 - long-long-float:show-notice-about-enum-with-debug, r=pnkfelix

Show notice about  "never used" of Debug for enum

Close rust-lang#123068

If an ADT implements `Debug` trait and it is not used, the compiler says a note that indicates intentionally ignored during dead code analysis as [this note](https://github.com/rust-lang/rust/blob/2207179a591f5f252885a94ab014dafeb6e8e9e8/tests/ui/lint/dead-code/unused-variant.stderr#L9).
However this node is not shown for variants that have fields in enum. This PR fixes to show the note.
dgreid added a commit to dgreid/salus that referenced this issue Apr 16, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
dgreid added a commit to dgreid/salus that referenced this issue Apr 16, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
dgreid added a commit to dgreid/salus that referenced this issue Apr 17, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
dgreid added a commit to dgreid/salus that referenced this issue Apr 17, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
dgreid added a commit to dgreid/salus that referenced this issue Apr 17, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
dgreid added a commit to dgreid/salus that referenced this issue Apr 21, 2025
rust has decided that only using an item for its debug output is worthy
of a warning.

this is intentional: rust-lang/rust#123068

accept that and mark the error enums as allowing dead code to shut it
up. this is unfortunate as actual dead code warnings would be nice. If
that's a problem, we can add manual display implementations later.

Signed-off-by: Dylan Reid <dgreid@rivosinc.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.
Projects
None yet
7 participants