HomePhabricator

[Diagnostics] Bitwise negation of a boolean expr always evaluates to true; warn…

Description

[Diagnostics] Bitwise negation of a boolean expr always evaluates to true; warn with -Wbool-operation

Requested here:
http://lists.llvm.org/pipermail/cfe-dev/2019-October/063452.html

Details

Committed
xbolva00Oct 3 2019, 8:17 AM
Parents
rL373613: Fix MSVC "result of 32-bit shift implicitly converted to 64 bits" warning. NFCI.
Branches
Unknown
Tags
Unknown

Event Timeline

FYI: This fires even if the result isn't converted to a boolean, e.g. on this example:

bool a, b, c;
if (a & b & ~c) {}

The wording warning implies that the value of c is ignored, which isn't true: that code is equivalent to if (a && b && !c).

Obviously the code should be written in the more obvious way instead :-) but if it's intended to flag such patterns, it'd be nice to add a test and/or comment to document that.

Yeah, I need to change a wording a bit.

bool b =
int i = ~b;

Gcc and Clang warns here - OK, I just have to improve the warning message a bit.

rtrieu added a subscriber: rtrieu.Oct 4 2019, 5:06 PM
rtrieu added inline comments.
/cfe/trunk/lib/Sema/SemaExpr.cpp
13480

Consider using Expr::isKnownToHaveBooleanValue() to check in languages without a bool type.