snafu icon indicating copy to clipboard operation
snafu copied to clipboard

Warn when an enum variant is unused

Open carols10cents opened this issue 5 years ago • 3 comments

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.

carols10cents avatar May 14 '20 01:05 carols10cents

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 avatar May 16 '20 13:05 shepmaster

rust-lang/rust#47851 will be very relevant

shepmaster avatar May 17 '20 19:05 shepmaster