Skip to content

Conversation

@ckandeler
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Christian Kandeler (ckandeler)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/165411.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp (+6)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp (+4-2)
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 [

@ckandeler ckandeler requested a review from localspook October 28, 2025 15:22
Comment on lines +272 to +277
case CK_BaseToDerived:
if (!needsConstCast(SourceType, DestType)) {
ReplaceWithNamedCast("static_cast");
return;
}
break;
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?)

@EugeneZelenko
Copy link
Contributor

Please update Release Notes.

@ckandeler ckandeler force-pushed the QTCREATORBUG-18270-clang-tidy-fixits branch from 2240190 to 60c1beb Compare October 29, 2025 17:52
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.

4 participants