Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
break;
case CK_BaseToDerived:
if (!needsConstCast(SourceType, DestType)) {
ReplaceWithNamedCast("static_cast");
return;
}
break;
Comment on lines +272 to +277
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a best practice to use static_cast for base to derived always?
I think under RTTI, we should use dynamic_cast and under non-RTTI, static_cast could be used also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the code uses an unchecked cast implies a priori knowledge about the safety to do so, so static_cast seems to be the correct equivalent. Using dynamic_cast would likely incur unwanted overhead.
(I'm not too familiar with the clang-tidy code base: Is it possible/recommended to suggest competing fixes? If so, what happens to the respective code if --fix is used?)

default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ Changes in existing checks
adding an option to allow pointer arithmetic via prefix/postfix increment or
decrement operators.

- Improved :doc:`google-readability-casting
<clang-tidy/checks/google/readability-casting>` check by adding a fix-it
for downcasts.

- Improved :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals
<clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals>` check:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
// CHECK-FIXES: b1 = static_cast<int>(b);

Y *pB = (Y*)pX;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
// CHECK-FIXES: Y *pB = static_cast<Y*>(pX);
Y &rB = (Y&)*pX;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
// CHECK-FIXES: Y &rB = static_cast<Y&>(*pX);

const char *pc3 = (const char*)cpv;
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [
Expand Down