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

Warn when an enum variant is unused #238

Open
carols10cents opened this issue May 14, 2020 · 3 comments
Open

Warn when an enum variant is unused #238

carols10cents opened this issue May 14, 2020 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@carols10cents
Copy link

What happened

I had an enum that derived Snafu, and I added a variant to it:

use snafu::Snafu;
use std::{io, path::PathBuf, sync::Arc};

#[derive(Debug, Snafu, Clone)]
enum MyError {
    UnableToRead {
        #[snafu(source(from(io::Error, Arc::new)))]
        source: Arc<io::Error>,
        path: PathBuf,
    },
}

I did not use this variant anywhere.

What I expected

Usually when I have an enum variant that's unused, Rust gives a dead_code warning about it:

use std::{io, path::PathBuf, sync::Arc};

enum MyError {
    UsingThisOne,
    UnableToRead {
        source: Arc<io::Error>,
        path: PathBuf,
    },
}

fn main() {
    let _f = MyError::UsingThisOne;
}

produces:

warning: variant is never constructed: `UnableToRead`
  --> src/main.rs:7:5
   |
7  | /     UnableToRead {
8  | |         source: Arc<io::Error>,
9  | |         path: PathBuf,
10 | |     },
   | |_____^
   |
   = note: `#[warn(dead_code)]` on by default

It'd be nice to have this warning for Snafu enum variants as well.

@shepmaster shepmaster added enhancement New feature or request help wanted Extra attention is needed labels May 16, 2020
@shepmaster shepmaster changed the title Feature request: Unused enum variant warning Warn when an enum variant is unused May 16, 2020
@shepmaster
Copy link
Owner

Hmm, this does seem valuable, but might be tricky and maybe outside of SNAFU's ability.

I think the problem is that we generate structs for each variant, and each struct has methods and/or trait implementations which generate that variant, which means that the variant isn't unused.

I think that the struct itself isn't marked as unused because nothing generated by a procedural macro is marked as unused. I don't know of a way to indicate otherwise.

@shepmaster
Copy link
Owner

rust-lang/rust#47851 will be very relevant

@mu001999
Copy link

This has been fixed recently, we can get:

warning: variant `UnableToRead` is never constructed
 --> src/main.rs:6:5
  |
5 | enum MyError {
  |      ------- variant in this enum
6 |     UnableToRead {
  |     ^^^^^^^^^^^^
  |
  = note: `MyError` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

with the input:

use snafu::Snafu;
use std::{io, path::PathBuf, sync::Arc};

#[derive(Debug, Snafu, Clone)]
enum MyError {
    UnableToRead {
        #[snafu(source(from(io::Error, Arc::new)))]
        source: Arc<io::Error>,
        path: PathBuf,
    },
    UsingThisOne,
}

fn main() {
    let _f = MyError::UsingThisOne;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants