[clang-tools-extra] [clang-tidy] Provide fix-its for downcasts in google-readability-casting (PR #165411)

Christian Kandeler via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 28 07:55:57 PDT 2025


https://github.com/ckandeler created https://github.com/llvm/llvm-project/pull/165411

None

>From 22401900785e0bc7c21d9880728a37225decb81d Mon Sep 17 00:00:00 2001
From: Christian Kandeler <christian.kandeler at qt.io>
Date: Tue, 28 Oct 2025 15:49:13 +0100
Subject: [PATCH] [clang-tidy] Provide fix-its for downcasts in
 google-readability-casting

---
 .../clang-tidy/google/AvoidCStyleCastsCheck.cpp             | 6 ++++++
 .../test/clang-tidy/checkers/google/readability-casting.cpp | 6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

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 [



More information about the cfe-commits mailing list