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 "never read" warning for field that is used by derive(Debug) #123068

Open
jkarneges opened this issue Mar 26, 2024 · 8 comments · May be fixed by #124460
Open

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

jkarneges opened this issue Mar 26, 2024 · 8 comments · May be 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.

@Nilstrieb
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.

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 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