[cfe-dev] Which floating point to bool conversions should trigger warnings?

Richard Trieu via cfe-dev cfe-dev at lists.llvm.org
Fri Apr 22 15:47:59 PDT 2016

I recently added (r267054) and then revert (r267234) some warnings for
floating point to bool conversions pending more discussion on the topic.
>From my experience, bool conversions are tricky and a source of many bugs,
so a warning in this place would be good.

Bool behaves a little differently than other integer types.  For instance,
0.5 is converted to zero for integer types, except for bool which it gets
converted to true.  Also, bool is the type for conditionals and resulting
type of logical operators.

However, at least one style guide says to use floating point to bool
conversion (https://webkit.org/code-style-guidelines/#null-false-and-zero)

With that, I hope to get a little more discussion on this topic before
implementing the floating point to bool conversion warnings again.  The two
factors for the warning are the source of the floating point value and
where the conversion happens, although there may be other factors we need
to take into account.  Only the source has been taken account so far since
it is the easier way to implement.

Exact floating point literals (0.0, 1.0)
Rounded floating point literals (0.5, 4.0)
Exact compiler time constant (5.0 - 4.0, kZero)
Inexact compiler time constant (1.0 / 2.0, kHugeNumber)
Run time values (getFloat())

Function Argument
Assigning/Initializing bool variable
Return statement of bool returning function
Condition of if statement, for loop, while loop, or do-while loop
Condition of conditional operator (?:)
Operand of logical operators(&&, ||, !)

Currently, exact floating point literals are not warned on, rounded
floating point literals are under -Wliteral-conversion, and everything else
is under -Wfloat-conversion.

Note that exclusionary groups may also be useful, for instance
"-Wfloat-conversion -Wno-some-bool-warning" if that helps filter out some
of the more noisy warnings.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160422/32bab8dd/attachment.html>

More information about the cfe-dev mailing list