Author: Christian Kandeler
Date: 2025-11-19T10:43:35+01:00
New Revision: 915e9adbe5d1c577a21ac8b495b7c54c465460fd

URL: 
https://github.com/llvm/llvm-project/commit/915e9adbe5d1c577a21ac8b495b7c54c465460fd
DIFF: 
https://github.com/llvm/llvm-project/commit/915e9adbe5d1c577a21ac8b495b7c54c465460fd.diff

LOG: [clang-tidy] Provide fix-its for casts to void* in 
google-readability-casting (#167655)

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp 
b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
index 47e859d21e451..c438889e22ab7 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -248,6 +248,12 @@ void AvoidCStyleCastsCheck::check(const 
MatchFinder::MatchResult &Result) {
       }
       break;
     }
+    if (DestType->isVoidPointerType() && SourceType->isPointerType() &&
+        !SourceType->getPointeeType()->isPointerType()) {
+      ReplaceWithNamedCast("reinterpret_cast");
+      return;
+    }
+
     [[fallthrough]];
   case clang::CK_IntegralCast:
     // Convert integral and no-op casts between builtin types and enums to

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 743397e3ec6ce..dc8abd88899a4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -426,7 +426,7 @@ Changes in existing checks
 
 - Improved :doc:`google-readability-casting
   <clang-tidy/checks/google/readability-casting>` check by adding fix-it
-  notes for downcasts.
+  notes for downcasts and casts to void pointer.
 
 - Improved :doc:`google-readability-todo
   <clang-tidy/checks/google/readability-todo>` check to accept the new TODO

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 f9feb8854249b..d8e8c5017a9b2 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
@@ -108,6 +108,10 @@ void f(int a, double b, const char *cpc, const void *cpv, 
X *pX) {
   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
   // CHECK-FIXES: Y &rB = static_cast<Y&>(*pX);
 
+  void *vp = (void *) pX;
+  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}; use reinterpret_cast
+  // CHECK-FIXES: void *vp = reinterpret_cast<void *>(pX);
+
   const char *pc3 = (const char*)cpv;
   // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [
   // CHECK-FIXES: const char *pc3 = static_cast<const char*>(cpv);


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to