[clang-tools-extra] [clang-tidy] Add check 'bugprone-cast-to-struct' (PR #153428)
Balázs Kéri via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 14 02:28:55 PDT 2025
================
@@ -0,0 +1,54 @@
+.. title:: clang-tidy - bugprone-cast-to-struct
+
+bugprone-cast-to-struct
+=======================
+
+Finds casts from pointers to struct or scalar type to pointers to struct type.
+
+Casts between pointers to different structs can be unsafe because it is possible
+to access uninitialized or undefined data after the cast. There may be issues
+with type compatibility or data alignment. Cast from a pointer to a scalar type
+(which points often to an array or memory block) to a `struct` type pointer can
+be unsafe for similar reasons. This check warns at casts from any non-`struct`
+type to a `struct` type. No warning is produced at cast from type `void *` (this
+is the usual way of allocating memory with `malloc`-like functions). It is
+possible to specify additional types to ignore by the check. In addition,
+`union` types are completely excluded from the check. The check does not take
+into account type compatibility or data layout, only the names of the types.
+
+.. code-block:: c
+
+ void test1(char *p) {
+ struct S1 *s;
+ s = (struct S1 *)p; // warn: 'char *' is converted to 'struct S1 *'
+ }
+
+ void test2(struct S1 *p) {
+ struct S2 *s;
+ s = (struct S2 *)p; // warn: 'struct S1 *' is converted to 'struct S2 *'
+ }
+
+ void test3(void) {
+ struct S1 *s;
+ s = (struct S1 *)calloc(1, sizeof(struct S1)); // no warning
+ }
+
+Options
+-------
+
+.. option:: IgnoredFromTypes
+
+ Semicolon-separated list of types for which the checker should not warn if
+ encountered at cast source. Can contain regular expressions. The `*`
+ character (for pointer type) is not needed in the type names.
+
+.. option:: IgnoredToTypes
+
+ Semicolon-separated list of types for which the checker should not warn if
+ encountered at cast destination. Can contain regular expressions. The `*`
+ character (for pointer type) is not needed in the type names.
+
+.. option:: IgnoredFunctions
----------------
balazske wrote:
I added the ignored function option to remove false positives coming from system headers like **xmmintrin.h** from many functions like this:
```c
static __inline__ __m128 __DEFAULT_FN_ATTRS
_mm_loadu_ps(const float *__p)
{
struct __loadu_ps {
__m128_u __v;
} __attribute__((__packed__, __may_alias__));
return ((const struct __loadu_ps*)__p)->__v; // warning: cast from 'const float *' to 'const struct __loadu_ps *'
}
```
Probably the type name `__loadu_ps` or similar can be enough, or the case can be detected in other way (check for specific function attributes like `nodebug` which is defined in `__DEFAULT_FN_ATTRS` can work for this case). I wanted to have a solution to remove similar warnings. Sometimes such casts appear in specific macros, then a similar option for macro names would be needed. Or just do not emit warnings if it comes from system header.
https://github.com/llvm/llvm-project/pull/153428
More information about the cfe-commits
mailing list