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 8637a9ab6d9f6..caa3224e0fff4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -404,7 +404,7 @@ Changes in existing checks - Improved :doc:`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 ` 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..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(*pX); + void *vp = (void *) pX; + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}; use reinterpret_cast + // CHECK-FIXES: void *vp = reinterpret_cast(pX); + const char *pc3 = (const char*)cpv; // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [ // CHECK-FIXES: const char *pc3 = static_cast(cpv);