[cfe-dev] Possibly invalid enum tautology warning

Jordy Rose jediknil at belkadan.com
Sun Aug 7 17:49:07 PDT 2011


I'm actually of the opinion that this IS tautological.

> enum tester { A = 0, B };
> 
> void show_tautology(enum tester test)
> {
>   if (test < 0)
>     printf("is the above test necessarily tautological?");
> }

By typing 'test' as a 'tester', you've implicitly promised the compiler that it will only take on values in the enumeration, i.e. A (0) or B (1). It doesn't actually matter what the representation is; these values are perfectly constrained by the C standard.

That is, I would argue that these are tautological as well:

enum foo { bar = -1, baz = -2 };
void testFoo (enum foo test)
{
  if (test < -1)
    printf("unreachable");
  if (test > 5)
    printf("unreachable");
}

This, on the other hand, would not be tautological:

enum { A = 0; }
void test(int i)
{
  if (i < A)
    printf("not tautological");
}

While promotion rules make all of these into int-int comparisons (either signed or unsigned), it seems that the warning is still correct, even if it shows up for the wrong reason. (I agree that we shouldn't really be warning just based on the signedness OR size of the enum.)

Maybe the right solution is to make a new warning for "tautological comparisons with enumeration types", put that under its own flag, and then silence the original warning if an enum type is involved.

Just my two cents,
Jordy



More information about the cfe-dev mailing list