Warning at always true condition

Hi All
Does clang support any switch to warn about a conditional eg in if(p* !=‘h’ || p* !=‘H’), which is always true.

Thankss in reply for timely response

Warm Reagrds

I think there’s an option -Wtautological-compare or something like that which might do what you want.

There's a -Weverything which I think you can use to check to see if a bit of code triggers any warnings.

Thanks Sean
I will have a look at that.
Thanks for the pointer


Sorry but true.
This is not what, I was looking.

char *p =“Hello World\n”;
if(p[0] != ‘h’ || p[0] != ‘H’)
cout << “Word does not start with h/H\n”;

The condition is always true and none of compiler seems to warn them.


Does Clang’s static analyzer find it?

I don't believe Clang (as a compiler anyway - as Sean mentioned, it
might be in the Static Analyzer, but even then I suspect not) has such
a warning. You could file a bug about it, but the real challenge will
be trying to get the noise down to an acceptable level to include this
warning in Clang. If you can provide examples of real bugs that such a
diagnostic would have found & include those details in the bug that'd
be really helpful.

- David

The analyzer will catch this (under the "deadcode" checkers, enabled by default), although in your particular test case it's actually smart enough to /know/ that p[0] is 'H' (because it can see the initialization).

Adding this to the compiler is certainly possible, at least not for the limited case of ($x != k1 || $x != k2). If you put another conditional in the middle ($x != k1 || $y || $x != k2) it's a bit harder to see immediately that this is always true. I can see how the original case would come up in real life with your example, though—in English you can say "*p is not 'h' or 'H'", but in C you have to say "*p is not 'h' AND *p is not 'H'".

If you decide to implement this yourself, watch out for when something in the condition has side effects—then it's /not/ guaranteed to be a tautology. If you were just asking, please file an "enhancement" bug at http://llvm.org/bugs/


Yes, this is actually the very purpose of -Weverything, it actives everything so you can discover new warnings.

If the output is too verbose, the diagnostics tell you what flags activated them so you can turn them off with the “no-” version: -Weverything -Wno-unused.

– Matthieu