cfe-dev Digest, Vol 50, Issue 23

Date: Mon, 8 Aug 2011 09:42:02 +0100
From: Peter Geoghegan <peter@2ndquadrant.com>
Subject: Re: [cfe-dev] Possibly invalid enum tautology warning
To: John McCall <rjmccall@apple.com>
Cc: cfe-dev@cs.uiuc.edu
Message-ID:
<CAEYLb_U66g2xNc1g_uoJRdErxsjpr-kO=6bEVmeS=ehs+Enfxw@mail.gmail.com>
Content-Type: text/plain; charset=windows-1252

I still consider this a useful warning, even on enum types. ?When I
was implementing it, I was persuaded that we ought to have a
special case for comparing against an enumerator which happens
to be zero, because that was indicative of a particular common
and harmless idiom (specifically, things like if (v < v_first || v > v_last)).
I’m loathe to extend that to a direct comparison against zero, where
it really does seem like the programmer is specifically testing for
an error case and might be surprised to realize that their test never
triggers. ?People who really do want to litter their code with defensive
checks like this should just disable the tautological comparison
check ? it’s not likely that it would only ever come up with enums.

Respectfully, I have to wonder if that’s a practical attitude. They
may not want to “litter” their code. They may just want to do this
exactly once, as for example Postgres was before a patch was committed
that sidestepped the issue. Should they still have to disable the
tautological comparison check?

Well, you’re assuming that comparisons like this are always in overly-
defensive code where the tautology is harmless.

No, I’m not. I’m quite simply taking issue with the fact that the
tautology exists only because of a factor that is implementation
defined. /The tautology is not essential to the code/.


Peter Geoghegan ? ? ? http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training and Services

Hello Peter:

/The tautology is not essential to the code/.

It is, actually (though reported for the wrong reasons).

The Standard mandates that all values that an enum may take should be comprised between the “min-value” and “max-value”. If Clang has used an unsigned type, then it means that there never was any value inferior to 0 in the enum definition, and therefore the comparison is tautological.

Do note that assigning a value outside [min-value, max-value] to the enum leads to unspecified behavior, and thus makes the code non-portable.

– Matthieu.