Skip to content

Fix #6: warn on c-style and function style casts that generate a "bit cast" in the clang tidy analysis. convert those to explicit reinterpret casts if possible - #8

Open
leftibot wants to merge 1 commit into
cpp-best-practices:mainfrom
leftibot:fix/issue-6-warn-on-c-style-and-function-style-casts
Open

leftibot wants to merge 1 commit into
cpp-best-practices:mainfrom
leftibot:fix/issue-6-warn-on-c-style-and-function-style-casts

Conversation

@leftibot

@leftibot leftibot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Automated fix by @leftibot.

What changed

Fix #6: Add prefer-reinterpret-cast check for C-style and function-style bit casts
C-style and function-style casts silently fall back to reinterpret semantics
when clang resolves them to a BitCast or LValueBitCast, hiding a type pun
behind innocuous syntax. The new clang-query check flags these casts,
including ones that also discard const (where the bit cast is an implicit
child of the explicit cast) and ones that only become bit casts in template
instantiations, so they can be rewritten as explicit reinterpret_cast.
Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com

Files

 .../prefer-reinterpret-cast.query                   | 13 +++++++++++++
 .../tests/fail/bit_cast_discarding_const.cpp        |  9 +++++++++
 .../tests/fail/bit_cast_in_template.cpp             |  6 ++++++
 .../tests/fail/c_style_pointer_bit_cast.cpp         |  4 ++++
 .../tests/fail/c_style_reference_bit_cast.cpp       |  4 ++++
 .../tests/fail/function_style_pointer_bit_cast.cpp  |  6 ++++++
 .../tests/pass/reinterpret_cast.cpp                 |  6 ++++++
 .../tests/pass/template_without_bit_cast.cpp        |  6 ++++++
 .../tests/pass/value_conversions.cpp                | 21 +++++++++++++++++++++
 9 files changed, 75 insertions(+)

Closes #6

Triggered by @lefticus.

…le and function-style bit casts

C-style and function-style casts silently fall back to reinterpret semantics
when clang resolves them to a BitCast or LValueBitCast, hiding a type pun
behind innocuous syntax. The new clang-query check flags these casts,
including ones that also discard const (where the bit cast is an implicit
child of the explicit cast) and ones that only become bit casts in template
instantiations, so they can be rewritten as explicit reinterpret_cast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

warn on c-style and function style casts that generate a "bit cast" in the clang tidy analysis. convert those to explicit reinterpret casts if possible

1 participant