I wonder why -Wsign-compare only warns when there is no int promotion?

No warning for this, where the result is “surprisingly” false because of int 
promotion:
   signed char i = (signed char) -3;
   unsigned char j = (unsigned char) -3;
   printf("i=%x j=%x i==j=%d\n", i, j, i==j);
gcc  -Wsign-compare ~/test.c -o /tmp/glop && /tmp/glop
i=fffffffd j=fd i==j=0
But a warning for this where the result is true (which I believe represents a 
lower risk than the above case):
    signed int i = (signed int) -3;
    unsigned int j = (unsigned int) -3;
    printf("i=%x j=%x i==j=%d\n", i, j, i==j);

% gcc  -Wsign-compare ~/test.c -o /tmp/glop && /tmp/glop
i=fffffffd j=fd i==j=0
/home/ddd/test.c:7:42: warning: comparison between signed and unsigned integer 
expressions [-Wsign-compare]
     printf("i=%x j=%x i==j=%d\n", i, j, i==j);
                                          ^~
i=fffffffd j=fffffffd i==j=1

I did not find a rationale in the documentation, nor a good alternative flag 
that would focus on comparisons with promotion.

Tested with several semi-old variants of GCC, e.g. 4.8.5, and more recent ones 
e.g. 7.2.1, with consistent results.
gcc --version
gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)


Thanks,
Christophe



Reply via email to