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

diagnostics: dead_code false negative on never constructed enum variant #76788

Closed
matthiaskrgr opened this issue Sep 16, 2020 · 0 comments · Fixed by #81310
Closed

diagnostics: dead_code false negative on never constructed enum variant #76788

matthiaskrgr opened this issue Sep 16, 2020 · 0 comments · Fixed by #81310
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Sep 16, 2020

use std::fmt;

trait CargoCacheError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
}

impl fmt::Display for dyn CargoCacheError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.fmt(f)
    }
}

#[derive(Debug, Eq, PartialEq)]
pub(crate) enum CacheError<'a> {
    SomeCache(&'a str), // WARNING HERE
    NoCache, // BUT NOT HERE
}

impl fmt::Display for CacheError<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            Self::NoCache => write!(f, ""),
            Self::SomeCache(string) => write!(f, "oh no, {}", string),
        }
    }
}

pub fn main() {}

playground

I expected to see this happen:
There should be a warning that CacheError::SomeCache as well as CacheError::NoCache are never constructed.

Instead, this happened:
Instead, there only was a warning about dead code for CacheError::NoCache

warning: variant is never constructed: `SomeCache`
  --> src/main.rs:27:5
   |
27 |     SomeCache(&'a str), // WARNING HERE
   |     ^^^^^^^^^^^^^^^^^^

rustc --version --verbose:

rustc 1.48.0-nightly (6af1bdda5 2020-09-15)
binary: rustc
commit-hash: 6af1bdda54abc9e919fc1137411dfc4311e05649
commit-date: 2020-09-15
host: x86_64-unknown-linux-gnu
release: 1.48.0-nightly
LLVM version: 11.0
@matthiaskrgr matthiaskrgr added the C-bug Category: This is a bug. label Sep 16, 2020
@matthiaskrgr matthiaskrgr changed the title diagnostics: dead_code false negative on never constructed variant diagnostics: dead_code false negative on never constructed enum variant Sep 16, 2020
@camelid camelid added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Sep 24, 2020
@bors bors closed this as completed in 04ddf42 Jan 25, 2021
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants