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

            Bug ID: 86647
           Summary: Test on constant expression (unsigned) -1 < 0 triggers
                    a spurious -Wtype-limits warning
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

(unsigned) -1 < 0 triggers a -Wtype-limits warning, while the gcc man page
says:

  -Wtype-limits
    Warn if a comparison is always true or always false due to the
    limited range of the data type, but do not warn for constant
    expressions.  For example, warn if an unsigned variable is compared
    against zero with "<" or ">=".  This warning is also enabled by
    -Wextra.

i.e. one shouldn't get a warning for constant expressions like (unsigned) -1.

int foo (void)
{
  return (unsigned) -1 < 0;
}

zira:~> gcc-snapshot -Wtype-limits -c tst.c
tst.c: In function 'foo':
tst.c:3:24: warning: comparison of unsigned expression < 0 is always false
[-Wtype-limits]
   return (unsigned) -1 < 0;
                        ^

Note that 1U < 0 does not trigger a warning, as expected. But 2147483648U < 0
triggers it (with 32-bit int). So, it seems that one gets a warning when the
unsigned constant converted to signed would give a negative integer.

The consequence of this bug is that the following macro used to detect signed
integer types

#define SIGNED(T) ((T) -1 < 0)

triggers a warning on unsigned integer types.

Reply via email to