Skip to content

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

Closed
@jkarneges

Description

@jkarneges
Contributor

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)

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 26, 2024
clubby789

clubby789 commented on Mar 26, 2024

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

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-diagnosticsArea: Messages for errors, warnings, and lints
D-confusingDiagnostics: Confusing error or lint that should be reworked.
and removed
C-bugCategory: This is a bug.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 26, 2024
jkarneges

jkarneges commented on Mar 26, 2024

@jkarneges
ContributorAuthor

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

krtab

krtab commented on Apr 3, 2024

@krtab
Contributor

@rustbot claim

krtab

krtab commented on Apr 5, 2024

@krtab
Contributor

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

rustbot

rustbot commented on Apr 5, 2024

@rustbot
Collaborator

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.

22 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @fjarri@jkarneges@long-long-float@clubby789@rustbot

    Issue actions

      field "never read" warning for field that is used by derive(Debug) · Issue #123068 · rust-lang/rust