https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91685
Bug ID: 91685
Summary: -Wtype-limits warns for constant expression
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: yann at droneaud dot fr
Target Milestone: ---
I'm was trying to implement a test for socklen_t type signedness, because on
some platform it's signed and some it's unsigned. For signed socklen_t, I
wanted to rejected negative values.
I found following code triggers a warning:
$ cat test.c
#include <stdbool.h>
bool test_unsigned(void)
{
return ((unsigned int)0 - 1) < 0;
}
$ gcc -O2 -Wall -Wextra -c test.c
warning: comparison of unsigned expression in '< 0' is always false
[-Wtype-limits]
According to the documentation for -Wtype-limits, this warning should not be
produced, because expression is constant:
> 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.
https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html#index-Wtype-limits
I've made some tests on godbolt, see https://godbolt.org/z/b1QqIC and found
clang doesn't trigger the warning.
Perhaps warning here is welcomed and documentation should be updated ?
In the mean time, I still looking for a portable way to test if socklen_t is
negative on platform where it's signed ...