CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
52 lines
2.3 KiB
ReStructuredText
52 lines
2.3 KiB
ReStructuredText
.. title:: clang-tidy - bugprone-casting-through-void
|
|
|
|
bugprone-casting-through-void
|
|
=============================
|
|
|
|
Detects unsafe or redundant two-step casting operations involving ``void*``,
|
|
which is equivalent to ``reinterpret_cast`` as per the
|
|
`C++ Standard <https://eel.is/c++draft/expr.reinterpret.cast#7>`_.
|
|
|
|
Two-step type conversions via ``void*`` are discouraged for several reasons.
|
|
|
|
- They obscure code and impede its understandability, complicating maintenance.
|
|
- These conversions bypass valuable compiler support, erasing warnings related
|
|
to pointer alignment. It may violate strict aliasing rule and leading to
|
|
undefined behavior.
|
|
- In scenarios involving multiple inheritance, ambiguity and unexpected
|
|
outcomes can arise due to the loss of type information, posing runtime
|
|
issues.
|
|
|
|
In summary, avoiding two-step type conversions through ``void*`` ensures
|
|
clearer code, maintains essential compiler warnings, and prevents ambiguity
|
|
and potential runtime errors, particularly in complex inheritance scenarios.
|
|
If such a cast is wanted, it shall be done via ``reinterpret_cast``,
|
|
to express the intent more clearly.
|
|
|
|
Note: it is expected that, after applying the suggested fix and using
|
|
``reinterpret_cast``, the check
|
|
:doc:`cppcoreguidelines-pro-type-reinterpret-cast
|
|
<../cppcoreguidelines/pro-type-reinterpret-cast>` will emit a warning.
|
|
This is intentional: ``reinterpret_cast`` is a dangerous operation that can
|
|
easily break the strict aliasing rules when dereferencing the casted pointer,
|
|
invoking Undefined Behavior. The warning is there to prompt users to carefully
|
|
analyze whether the usage of ``reinterpret_cast`` is safe, in which case the
|
|
warning may be suppressed.
|
|
|
|
Examples:
|
|
|
|
.. code-block:: c++
|
|
|
|
using IntegerPointer = int *;
|
|
double *ptr;
|
|
|
|
static_cast<IntegerPointer>(static_cast<void *>(ptr)); // WRONG
|
|
reinterpret_cast<IntegerPointer>(reinterpret_cast<void *>(ptr)); // WRONG
|
|
(IntegerPointer)(void *)ptr; // WRONG
|
|
IntegerPointer(static_cast<void *>(ptr)); // WRONG
|
|
|
|
reinterpret_cast<IntegerPointer>(ptr); // OK, clearly expresses intent.
|
|
// NOTE: dereferencing this pointer violates
|
|
// the strict aliasing rules, invoking
|
|
// Undefined Behavior.
|