-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Provide fix-its for casts to void* in google-readability-casting #167655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+11
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@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:
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);
|
vbvictor
reviewed
Nov 13, 2025
clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp
Outdated
Show resolved
Hide resolved
zwuis
reviewed
Nov 14, 2025
26a2f80 to
3657a24
Compare
Member
Author
|
Addressed review comments. |
vbvictor
approved these changes
Nov 18, 2025
Contributor
vbvictor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.