Is there a way to make clang (or gcc) warn you if you use integer constants with boolean operators? Specifically (but not only) in an if
context?
What I mean is, I wrote:
enum MatchType {MATCHED, FAKE, DUMMY};
if (matchType == MatchType::FAKE || MatchType::DUMMY) {
// ...
}
where it should have been
if (matchType == MatchType::FAKE || matchType == MatchType::DUMMY) {
The OR operation with a constant clearly doesn't make sense (especially when it is not a literal 0 or 1). Any idea how to prevent this? (I know using switch
would be the right thing to do in this specific case, and would prevent this kind of mistake, but I'm still interested in an answer.)
The similar question "is there a gcc warning for conditional expression is constant" is focused on the case where you have an expression in an if
that - after simplification - is constant. I'm asking about the case where you use boolean operators on non-bool constants, for example:
bool result = flags && 42; // you might have meant `&`
Also, in this case the conditional expression is not constant (depends on a
), but it is probably still a mistake:
if (a == 5 && 6) { /*...*/ }