Skip to content

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

Closed
@jkarneges

Description

@jkarneges

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)

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

Development

No branches or pull requests

Issue actions