-
Notifications
You must be signed in to change notification settings - Fork 15k
[clang-tidy] Provide fix-its for downcasts in google-readability-casting #165411
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
base: main
Are you sure you want to change the base?
[clang-tidy] Provide fix-its for downcasts in google-readability-casting #165411
Conversation
|
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Christian Kandeler (ckandeler) ChangesFull diff: https://github.com/llvm/llvm-project/pull/165411.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
index 174ecb0ed7b77..80c53de0cf237 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -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;
default:
break;
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp
index 7ccdf705e8399..f9feb8854249b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp
@@ -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 [
|
| case CK_BaseToDerived: | ||
| if (!needsConstCast(SourceType, DestType)) { | ||
| ReplaceWithNamedCast("static_cast"); | ||
| return; | ||
| } | ||
| break; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?)
|
Please update Release Notes. |
2240190 to
60c1beb
Compare
No description provided.