adwhit / diesel-derive-enum

Use enums with Diesel ORM
Apache License 2.0
270 stars 30 forks source link

Empty enum produces warning #48

Open l4l opened 4 years ago

l4l commented 4 years ago

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
    }
}
adwhit commented 4 years ago

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

l4l commented 4 years ago

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.

adwhit commented 4 years ago

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.