diesel-derive-enum icon indicating copy to clipboard operation
diesel-derive-enum copied to clipboard

Empty enum produces warning

Open l4l opened this issue 4 years ago • 3 comments

The following code:

#[derive(Debug, diesel_derive_enum::DbEnum, diesel::SqlType)]
pub enum A {}

will lead to compiler warning:

warning: unreachable expression
 --> src/lib.rs:1:17
  |
1 | #[derive(Debug, diesel_derive_enum::DbEnum, diesel::SqlType)]
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                 |
  |                 unreachable expression
  |                 any code following this expression is unreachable

Particularly, if expand the macro issue is clearly seen at ToSql<AMapping, DB>:

impl<DB: Backend> ToSql<AMapping, DB> for A {
    fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
        match *self {}
        Ok(IsNull::No) // <- that one is unreachable
    }
}

l4l avatar May 10 '20 15:05 l4l

Hmm. What do you think should happen with an empty enum, hard failure? I agree that a warning isn't very helpful.

adwhit avatar May 10 '20 18:05 adwhit

Well, I actually found it out by an accident, but probably it can be useful with "never" type, e.g. in postgres: CREATE TYPE NeverType AS ENUM (). Though, I'm not even sure if it works with diesel properly, but probably might be handy, if so.

l4l avatar May 10 '20 19:05 l4l

Yes, I suppose the easiest thing to do it just remove the match block if there are no variants. No idea if that is really useful to anyone but it would remove the warning.

adwhit avatar May 25 '20 18:05 adwhit