1 .. title:: clang-tidy - bugprone-casting-through-void
3 bugprone-casting-through-void
4 =============================
6 Detects unsafe or redundant two-step casting operations involving ``void*``.
8 Two-step type conversions via ``void*`` are discouraged for several reasons.
10 - They obscure code and impede its understandability, complicating maintenance.
11 - These conversions bypass valuable compiler support, erasing warnings related
12 to pointer alignment. It may violate strict aliasing rule and leading to
14 - In scenarios involving multiple inheritance, ambiguity and unexpected outcomes
15 can arise due to the loss of type information, posing runtime issues.
17 In summary, avoiding two-step type conversions through ``void*`` ensures clearer code,
18 maintains essential compiler warnings, and prevents ambiguity and potential runtime
19 errors, particularly in complex inheritance scenarios.
25 using IntegerPointer = int *;
28 static_cast<IntegerPointer>(static_cast<void *>(ptr)); // WRONG
29 reinterpret_cast<IntegerPointer>(reinterpret_cast<void *>(ptr)); // WRONG
30 (IntegerPointer)(void *)ptr; // WRONG
31 IntegerPointer(static_cast<void *>(ptr)); // WRONG