-
Notifications
You must be signed in to change notification settings - Fork 13.3k
dead_code false positive on matched enum #29064
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
Comments
Warning is intentional, as value of enum is never constructed and match arms can't be reached. It actually is the case that "you can comment this out". |
If you comment out the enum, you have to comment out the match as well, otherwise it doesn't compile. So it doesn't seem very dead, to me. |
See also #27559. |
It is this way intentionally, exactly to catch variants that are not created anywhere. |
Ok, thanks for the explanation! At first I thought "but what if someone else instantiates one?" but if you make it |
Here is real code this affects. It's part of a macro-ization of the manual implementation of serde::Serialize. Grep for #[repr(u16)]
#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
pub enum State { $($fe),* , Finished }
pub struct MapVisitor<'a> {
pub value: &'a super::$name,
pub state: State,
}
impl<'a> ::serde::ser::MapVisitor for MapVisitor<'a> {
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: ::serde::Serializer
{
match self.state {
$(State::$fe => {
self.state = unsafe { ::std::mem::transmute(self.state as u16 + 1) };
Ok(Some(try!(serializer.visit_struct_elt(stringify!($fe), &self.value.$fe))))
})*
State::Finished => {
Ok(None)
}
}
}
}
} Later the enum is instantiated with a transmutation of 0. The error still occurs, even though every single enum variant actually gets used. |
That's your problem. Just use |
This code produces a dead code warning for the entire enum, but it isn't dead because the match expression refers to it. Or do I have the wrong definition of "dead"? (I generally assume when rustc says "this is dead" it means "you can comment this out".)
The text was updated successfully, but these errors were encountered: