Skip to content

Commit 26a2f80

Browse files
committed
[clang-tidy] Provide fix-its for casts to void* in google-readability-casting
1 parent da9015a commit 26a2f80

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
157157
<< FixItHint::CreateRemoval(ReplaceRange);
158158
return;
159159
}
160+
if (DestType->isVoidPointerType() && SourceType->isPointerType() &&
161+
!SourceType->getPointeeType()->isPointerType()) {
162+
diag(CastExpr->getBeginLoc(), "redundant cast to void *")
163+
<< FixItHint::CreateRemoval(ReplaceRange);
164+
return;
165+
}
160166
}
161167

162168
// The rest of this check is only relevant to C++.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. If you want to modify sections/contents permanently, you should modify both
1+
#.. If you want to modify sections/contents permanently, you should modify both
22
ReleaseNotes.rst and ReleaseNotesTemplate.txt.
33

44
====================================================
@@ -404,7 +404,7 @@ Changes in existing checks
404404

405405
- Improved :doc:`google-readability-casting
406406
<clang-tidy/checks/google/readability-casting>` check by adding fix-it
407-
notes for downcasts.
407+
notes for downcasts and casts to void pointer.
408408

409409
- Improved :doc:`llvm-prefer-isa-or-dyn-cast-in-conditionals
410410
<clang-tidy/checks/llvm/prefer-isa-or-dyn-cast-in-conditionals>` check:

clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
108108
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast {{.*}}
109109
// CHECK-FIXES: Y &rB = static_cast<Y&>(*pX);
110110

111+
void *vp = (void *) pX;
112+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant cast
113+
// CHECK-FIXES: void *vp = pX;
114+
111115
const char *pc3 = (const char*)cpv;
112116
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [
113117
// CHECK-FIXES: const char *pc3 = static_cast<const char*>(cpv);

0 commit comments

Comments
 (0)