Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst

raw
1 .. title:: clang-tidy - bugprone-casting-through-void
2
3 bugprone-casting-through-void
4 =============================
5
6 Detects unsafe or redundant two-step casting operations involving ``void*``.
7
8 Two-step type conversions via ``void*`` are discouraged for several reasons.
9
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
13 undefined behavior.
14 - In scenarios involving multiple inheritance, ambiguity and unexpected outcomes
15 can arise due to the loss of type information, posing runtime issues.
16
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.
20
21 Examples:
22
23 .. code-block:: c++
24
25 using IntegerPointer = int *;
26 double *ptr;
27
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