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

Unexpected dead_code warning when returning Result type from main #123418

Open
zschreur opened this issue Apr 3, 2024 · 6 comments
Open

Unexpected dead_code warning when returning Result type from main #123418

zschreur opened this issue Apr 3, 2024 · 6 comments
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zschreur
Copy link

zschreur commented Apr 3, 2024

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>

@zschreur zschreur added the C-bug Category: This is a bug. label Apr 3, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 3, 2024
@shepmaster

This comment was marked as resolved.

@zschreur

This comment was marked as resolved.

@jieyouxu jieyouxu added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 3, 2024
@long-long-float
Copy link
Contributor

@rustbot claim

@krtab
Copy link
Contributor

krtab commented Apr 4, 2024

This is the same diagnostic issue as #123068 to which I am assigned I believe.

@long-long-float I haven't started working on it yet, so you can take both if you want.

@long-long-float
Copy link
Contributor

@krtab Thank you for informing me. I want to work on the issue #123068 in one PR or divied two PRs.

@long-long-float
Copy link
Contributor

As discussed in #88900, this behavior is intentional. However there is a bug not to show an explanation about this behavior. I fixed the bug at #124460 and the message will be shown.

warning: field `0` is never read
 --> ../rust/test.rs:3:11
  |
3 |     IOErr(std::io::Error),
  |     ----- ^^^^^^^^^^^^^^
  |     |
  |     field in this variant
  |
  = note: `MyError` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-dead_code Lint: dead_code S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants