>>> patrick keshishian 31-Aug-17 20:40 >>>
>
> On Thu, Aug 31, 2017 at 12:13:19PM -0700, William Ahern wrote:
> > On Thu, Aug 31, 2017 at 02:08:07PM +0200, Otto Moerbeek wrote:
> > > Hi,
> > > 
> > > /usr/src/usr.sbin/sasyncd/carp.c:157:12: warning: comparison of
> > > unsigned enum expression < 0 is always false [-Wtautological-compare]
> > >         if (state < 0 || state > FAIL)
> > >             ~~~~~ ^ ~
> > > /usr/src/usr.sbin/sasyncd/carp.c:166:20: warning: comparison of
> > > unsigned enum expression < 0 is always false [-Wtautological-compare]
> > >         if (current_state < 0 || current_state > FAIL) {
> > >             ~~~~~~~~~~~~~ ^ ~
>
>       if (!(state >= INIT && state <= FAIL))
>               state = FAIL;
> and
>       if (!(current_sate >= INIT && current_state <= FAIL) {
>               log_err ...
>               ...
>               return;
>       }
>
> More better?

If the compiler is using an unsigned underlying type for the enum, the
comparison

    state >= INIT

will give something along the lines of "expression >= 0 is always true"

I don't know what the best solution is here; I would defer to Otto!

Tom

> --patrick
>
> > > 
> > > this warning is a tiny bit interesting. A compiler is free to choose
> > > the type of the enum, as long as it can represent all given values.
> > > So another compiler might choose not to make it unsigned. So I came up
> > > with this fix that is not depending on the signedness of the type. But
> > > most of the time avoiding enum is better, I suppose.
> > 
> > It's free to choose the integer type of the enum, but enumeration members
> > (i.e. the constant identifiers) have int type.
> > 
> >   The identifiers in an enumerator list are declared as constants that have
> >   type int and may appear wherever such are permitted.
> > 
> >   C11 (N1570) 6.7.2.2p3.
> > 
> > Furthermore, the defining expression of the constant must be representable
> > as an int. 6.7.2.2p2.
> > 
> > I've always vascillated about which operand to cast, and to which type, when
> > silencing compilers' annoying warnings. But now that I've read the section
> > more closely I think I'll just cast the operand with enum type to int, all
> > things being equal.
> > 

Reply via email to