Warn when an enum variant is unused
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.
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.
rust-lang/rust#47851 will be very relevant