https://github.com/ckandeler updated https://github.com/llvm/llvm-project/pull/165411
>From 2920d957c5874362f86e9493df8fe4a6df962c38 Mon Sep 17 00:00:00 2001 From: Christian Kandeler <[email protected]> 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 ++++++ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../test/clang-tidy/checkers/google/readability-casting.cpp | 6 ++++-- 3 files changed, 14 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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 061fb114276dc..2002378a3b80f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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 fix-it + notes for downcasts. + - Improved :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals <clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals>` check: 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 [ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
