From: Jeff King Date: Mon, 07 Nov 2011 16:38:23 GMT Subject: Re: [PATCH 3/3] grep: get rid of useless x < 0 comparison on an enum member Message-ID: <20111107163823.GB27055@sigill.intra.peff.net> In-Reply-To: On Mon, Nov 07, 2011 at 02:12:28PM +0100, Andreas Schwab wrote: > Ævar Arnfjörð Bjarmason writes: > > > I.e. we'll always have GREP_HEADER_AUTHOR = 0 and > > GREP_HEADER_COMMITTER = 1, we'll never have GREP_HEADER_AUTHOR = and > > GREP_HEADER_COMMITTER = . > > That is irrelevant. You can always assign -1 to an object of enumerated > type and the implicit conversion to the underlying type is fully > defined, no matter what type the compiler choses. Yes, but now you are getting into implementation-defined behavior, which is also something to avoid. IOW, I don't think it's wrong for a static analyzer to complain about: if (enum_with_only_positive_values < 0) just because you could say: enum_with_only_positive_values = -1; and it would work on _some_ platforms. That same static analyzer should be complaining about the second line, which is where the real potential bug is. If you want "-1" as an enumerated value, then add it to your enum definition and the compiler will do the right thing. It is perfectly fine to do: enum foo { a = -1, b, c }; ... if (foo < 0) ... and the static analyzer should not complain about the conditional there. -Peff