This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Request for warnings on implicit bool to int conversions

Gabriel Dos Reis <> writes:

> I am trying to understand what the real issue is here.  Do you want
> -Wimplicit-char-to-int to?  -Wimplicit-short-to-int?  If not, why?
> where to stop?

I think it's more about conversion *to* bool than from bool, and it
catches places where code has been partly converted to bool (generally
because it predated C99) but the error conversion wasn't done properly.
For example, while returning true or false is common, returning 0 or -1 as
a boolean return value where 0 is success is also common (as with UNIX
library interfaces).  If someone messes up such a conversion and has a:

    status = -1;

somewhere, they get entirely the wrong result if status becomes a bool.
This warning would pick up cases like that.

The warnings about doing things like:

    bool b = true;
    b += 3;

are somewhat akin to the warnings about doing arithmetic on void *
pointers -- code like that is possibly a sign that there's something
flawed with the algorithm and it should be rewritten to treat booleans as
actual booleans.  (For example, b++ could easily wrap, and unexpectedly
fast depending on the size of bool on a platform.)

Russ Allbery (             <>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]