Skip to content

Unexpected dead_code warning when returning Result type from main #123418

Open

Description

I tried this code:

#[derive(Debug)]
enum MyError {
    IOErr(std::io::Error),
}

fn main() -> Result<(), MyError> {
    let io_err = std::io::Error::new(std::io::ErrorKind::Other, "Some IO Error");
    Err(MyError::IOErr(io_err))
}

I expect this to behave like so:

$ rustc ./main.rs
$ ./main
Error: IOErr(Custom { kind: Other, error: "Some IO Error" })

What I get is:

$ rustc ./main.rs
warning: field `0` is never read
 --> ./main.rs:3:11
  |
3 |     IOErr(std::io::Error),
  |     ----- ^^^^^^^^^^^^^^
  |     |
  |     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
  |
3 |     IOErr(()),
  |           ~~

warning: 1 warning emitted

$ ./main
Error: IOErr(Custom { kind: Other, error: "Some IO Error" })

Meta

rustc --version --verbose:

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-apple-darwin
release: 1.77.1
LLVM version: 17.0.6
Backtrace

<backtrace>

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

Metadata

Labels

A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-dead_codeLint: dead_codeS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions