Skip to content

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

Closed
durka opened this issue Oct 15, 2015 · 7 comments
Closed

dead_code false positive on matched enum #29064

durka opened this issue Oct 15, 2015 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@durka
Copy link
Contributor

durka commented Oct 15, 2015

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".)

enum Color {
    Brown,
    Green,
}

fn main() {
    let x = None;
    match x {
        Some(t) => match t {
            Color::Brown => println!("it's brown"),
            Color::Green => println!("it's green"),
        },
        None => println!("nothing"),
    }
}
@sanxiyn
Copy link
Member

sanxiyn commented Oct 15, 2015

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".

@durka
Copy link
Contributor Author

durka commented Oct 15, 2015

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.

@huonw huonw added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Oct 15, 2015
@sanxiyn
Copy link
Member

sanxiyn commented Oct 15, 2015

See also #27559.

@arielb1
Copy link
Contributor

arielb1 commented Oct 16, 2015

@durka

It is this way intentionally, exactly to catch variants that are not created anywhere.

@durka
Copy link
Contributor Author

durka commented Oct 16, 2015

Ok, thanks for the explanation! At first I thought "but what if someone else instantiates one?" but if you make it pub the misleading message goes away, so it's unlikely to affect real code.

@durka durka closed this as completed Oct 16, 2015
@apoelstra
Copy link
Contributor

Here is real code this affects. It's part of a macro-ization of the manual implementation of serde::Serialize. Grep for impl<'a> serde::ser::MapVisitor for PointMapVisitor<'a> to find the relevant code, which uses an integer to act as a state machine. In macros you cannot get a list of consecutive numbers like this, so I used an enum as:

            #[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.

@arielb1
Copy link
Contributor

arielb1 commented Jul 19, 2017

Later the enum is instantiated with a transmutation of 0.

That's your problem. Just use allow(dead_code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

5 participants