https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125261
Bug ID: 125261
Summary: _Generic -Wuseless-cast false positive
Product: gcc
Version: 16.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: eggert at cs dot ucla.edu
Target Milestone: ---
I ran into this problem when developing Gnulib code. This is gcc (GCC) 16.1.1
20260501 (Red Hat 16.1.1-1). With the following program t.c:
int
main (void)
{
return _Generic (0, int: 0, default: (int) 1);
}
the command:
gcc -Wuseless-cast t.c
outputs the following:
t.c: In function ‘main’:
t.c:4:40: warning: useless cast to type ‘int’ [-Wuseless-cast]
4 | return _Generic (0, int: 0, default: (int) 1);
| ^
The warning is a false alarm because the corresponding expression is not
evaluated, as per the semantics of _Generic.
These false alarms are making it hard to write macros and generic code that is
quiet in the presence of -Wuseless-cast. For example, one cannot write
something like this:
#define convert(t,e) _Generic (e, t: e, default: (t) (e))
because one gets a warning about a useless cast even when the cast isn't
evaluated.