[PATCH] New warning for mismatch between not and and/or operators.

Richard Trieu rtrieu at google.com
Mon Oct 13 19:45:25 PDT 2014


>>! In D5765#3, @arthur.j.odwyer wrote:
> Test cases?
Forgot to add the test case so the diff didn't pick it up.  Test is included now.
> 
>>Examples:
>>int x = ~y | ~z;
>>Fixed with | => ||
> 
> The code `~y | ~z` looks reasonable to me. It's equivalent to `~(y & z)`, a.k.a. "all the flags that aren't part of either `y` or `z`".
> Did you mean to denigrate `!y | !z` instead? In //that// case, the fixit `|` => `||` would definitely be appropriate IMHO.
Fixed the test case.  I intended to go `||` to `|` in the example.
> 
>>bool a = ~b && !c
>>Fixed with ~b => !b
> 
> I wouldn't expect the compiler to suggest a fixit in this case, but if it did, this code is sufficiently confusing that I personally would rather see the compiler suggest `~b` => `(b != -1)`, rather than suggesting a fixit that quietly changes the meaning of the code.

Both operator~ and operator! are called the not operator and the keys are right next to each other on US keyboards, so it would be easy to confuse the two.  From my testing, this was a common mistake of using `!` when `~` was intended in bitwise contexts.

http://reviews.llvm.org/D5765






More information about the cfe-commits mailing list