https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82272

            Bug ID: 82272
           Summary: RFE: request a warning for (<nonbool> == <bool>) etc.
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hpa at zytor dot com
  Target Milestone: ---

People not familiar with C, or who have read misguided style guides, sometimes
write code like:

    if (cond == true) {
        ....
    }

instead of

    if (cond) {
        ....
    }

which has the potential to be disastrous if "cond" is a numeric type which is
*not* a bool as these two statements are distinctly different.

As such, it would be a very good thing if a warning could be emitted in this
case.

C99 and C11 requires that <stdbool.h> define true and false as "the integer
constant 1" and "the integer constant 0", respectively, and that those
expressions are usable in the preprocessor.  I don't *believe* that that means
that they have to expand to the *tokens* 1 and 0, however (and there is always
the possibility to do a GNU extension.)

What I'm suggesting is to do something like _True and _False as cpp-safe
equivalent to (_Bool)1 and (_Bool)0, and warn on comparison between a _Bool and
a non-_Bool.

Reply via email to