https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117945
Bug ID: 117945
Summary: -Wuseless-cast could be suppressed when casting
to/from a type expanded from typedef or macro
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: k.ponikwicki17 at gmail dot com
Target Milestone: ---
This is not strictly a bug, more of a suggestion (debatable).
In case of typedef, -Wuseless-cast checks the underlying type,
and because often multiple typedef's share the same type under the hood,
said flag issues the warnings about them which are false-positive in most
cases.
For example, casting from uint64_t to size_t
will trigger the warning on most 64bit machines but not on 32bit
https://godbolt.org/z/j7ooEG5qd
$ cat main.c && gcc main.c -Wuseless-cast && uname -om
#include <stdlib.h> // size_t
#include <stdint.h> // uint64_t
int main()
{
uint64_t a;
size_t b = (size_t)a;
}
main.c: In function ‘main’:
main.c:6:16: warning: useless cast to type ‘long unsigned int’ [-Wuseless-cast]
6 | size_t b = (size_t)a;
| ^
x86_64 GNU/Linux
Generally, this warning will be almost always unwanted
while casting between types declared in System Headers
(unless doing something like size_t to/from size_t cast).
I would suggest suppressing the warning in said cases
or allow for alternative warning flag (or additional option for existing flag).
Potentially related to:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85043
(^ this bug describe alternative -Wcast-to-the-same-type warning)
Tested on: gcc 14.2.1 Linux