Skip to content

Conversation

@ckandeler
Copy link
Member

@ckandeler ckandeler commented Nov 12, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2025

@llvm/pr-subscribers-clang-tidy

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

Author: Christian Kandeler (ckandeler)

Changes

…-casting


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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp (+6)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+2-2)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp (+4)
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
index 47e859d21e451..c99e917607309 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -157,6 +157,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
           << FixItHint::CreateRemoval(ReplaceRange);
       return;
     }
+    if (DestType->isVoidPointerType() && SourceType->isPointerType() &&
+        !SourceType->getPointeeType()->isPointerType()) {
+      diag(CastExpr->getBeginLoc(), "redundant cast to void *")
+          << FixItHint::CreateRemoval(ReplaceRange);
+      return;
+    }
   }
 
   // The rest of this check is only relevant to C++.
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8637a9ab6d9f6..65feed572f3bc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -1,4 +1,4 @@
-.. If you want to modify sections/contents permanently, you should modify both
+#.. If you want to modify sections/contents permanently, you should modify both
    ReleaseNotes.rst and ReleaseNotesTemplate.txt.
 
 ====================================================
@@ -404,7 +404,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:`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 f9feb8854249b..1ea137a52c27a 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: redundant cast
+  // CHECK-FIXES: void *vp = 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);

@ckandeler ckandeler requested a review from vbvictor November 12, 2025 08:58
@localspook localspook changed the title [clang-tidy] Provide fix-its for casts to void* in google-readability… [clang-tidy] Provide fix-its for casts to void* in google-readability-casting Nov 12, 2025
@ckandeler ckandeler force-pushed the clang-tidy-casts-to-void-ptr branch from 26a2f80 to 3657a24 Compare November 14, 2025 11:55
@ckandeler
Copy link
Member Author

Addressed review comments.

Copy link
Contributor

@vbvictor vbvictor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ckandeler ckandeler merged commit 915e9ad into llvm:main Nov 19, 2025
12 checks passed
@ckandeler ckandeler deleted the clang-tidy-casts-to-void-ptr branch November 19, 2025 09:43
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