https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52763
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Last reconfirmed| |2019-10-19
Ever confirmed|0 |1
--- Comment #11 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #8)
> Clang warns when an enum object is compared to a constant that's out of the
> most restricted range of the enum's type. The warning is in -Wall. It
> doesn't warn when the object is compared to a constant that doesn't
> correspond to any of the type's enumerators. I can see that being useful to
> some (carefully written) projects but suspect it could be quite noisy for
> many others.
>
> $ cat t.C && clang++ -S -Wall -Wextra t.C
> enum E { NONE = 0, ONE = 1, TWO = 2 };
>
> bool f (E e)
> {
> return e == 3; // no warning here
> }
>
> bool g (E e)
> {
> return e == 4;
> }
>
>
> t.C:10:12: warning: comparison of constant 4 with expression of type 'E' is
> always false [-Wtautological-constant-out-of-range-compare]
> return e == 4;
> ~ ^ ~
> 1 warning generated.
I combined this testcase with the testcase in the original report and can
confirm that there is still no warning even after the addition of
-Wenum-conversion in bug 78736 (when compiling with either the C or C++
frontends)
$ cat 52763.cc
#ifdef __cplusplus
# include <cstdbool>
#else
# include <stdbool.h>
#endif /* __cplusplus */
typedef enum {
NONE = 0, ONE = 1, TWO = 2
} tEnumType;
bool f(tEnumType e)
{
return (e == 3); // no warning here
}
bool g(tEnumType e)
{
return (e == 4);
}
int main(void)
{
tEnumType var1 = TWO;
//Warn here, because we compare an enum to a non-enum type (1)
//should be 'if (var1 == ONE)'
if (var1 == 1)
return f(NONE);
else
return g(ONE);
}
$ /usr/local/bin/g++ -c -S -Wall -Wextra -Wconversion -pedantic 52763.cc
$ /usr/local/bin/gcc -c -S -Wall -Wextra -Wconversion -pedantic
-Wenum-conversion -Wc++-compat -x c 52763.cc
$
(no output)