[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
Tue Sep 9 06:44:50 PDT 2025


================
@@ -0,0 +1,60 @@
+.. 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. Cast from a
+scalar-type pointer (which points often to an array or memory block) to a
+``struct`` type pointer can be unsafe for similar reasons. This check warns at
+pointer 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). In addition, ``union`` types are excluded from the
+check. It is possible to specify additional types to ignore. 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
+   }
+
+Limitations
+-----------
+
+The check does run only on `C` code.
+
+C-style casts are discouraged in C++ and should be converted to more type-safe
+casts. The ``reinterpreted_cast`` is used for the most unsafe cases and
+indicates by itself a potentially dangerous operation. Additionally, inheritance
+and dynamic types would make such a check less useful.
----------------
balazske wrote:

Is this "Limitations" section needed? The documentation already shows that  the checker is for C only. Without more explanation it has no useful content.

https://github.com/llvm/llvm-project/pull/153428


More information about the cfe-commits mailing list