Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-tidy: add pro-type-cstyle-cast #1208

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Feb 24, 2022

Add cppcoreguidelines-pro-type-cstyle-cast:

This check flags all use of C-style casts that perform a static_cast downcast, const_cast, or reinterpret_cast.

Use of these casts can violate type safety and cause the program to access a variable that is actually of type X to be accessed as if it were of an unrelated type Z. Note that a C-style (T)expression cast means to perform the first of the following that is possible: a const_cast, a static_cast, a static_cast followed by a const_cast, a reinterpret_cast, or a reinterpret_cast followed by a const_cast. This rule bans (T)expression only when used to perform an unsafe cast.

https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-cstyle-cast.html

For an example how a c-style cast can accidentally cause a const cast, see: ECP-WarpX/WarpX#2889

@ax3l
Copy link
Member Author

ax3l commented Feb 24, 2022

I wonder what I do wrong that locally, this does not apply fixes:

share/openPMD/download_samples.sh build
chmod u-w build/samples/git-sample/*.h5

cmake -S . -B build           -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy-10);-system-headers=0;--fix" -DopenPMD_USE_INVASIVE_TESTS=ON
cmake --build build -- VERBOSE=ON

(I also tried the (older?) -fix.) Looks to me like this check should have a fixit: https://reviews.llvm.org/D14096

I see the warnings, but it does not fix the code automatically...

Add `cppcoreguidelines-pro-type-cstyle-cast`:
```
This check flags all use of C-style casts that perform a static_cast
downcast, const_cast, or reinterpret_cast.

Use of these casts can violate type safety and cause the program to
access a variable that is actually of type X to be accessed as if it
were of an unrelated type Z. Note that a C-style (T)expression cast
means to perform the first of the following that is possible: a
const_cast, a static_cast, a static_cast followed by a const_cast, a
reinterpret_cast, or a reinterpret_cast followed by a const_cast.
This rule bans (T)expression only when used to perform an unsafe
cast.
```
https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-type-cstyle-cast.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant