diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp index 76f2030158c81..2e7fc0f364b48 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp @@ -218,6 +218,11 @@ void AvoidCStyleCastCheck::check(const MatchFinder::MatchResult &Result) { ReplaceWithNamedCast("static_cast"); return; case CK_NoOp: + if (isa(CastExpr->getSubExprAsWritten()->IgnoreImpCasts())) { + ReplaceWithNamedCast("static_cast"); + return; +} + if (FnToFnCast) { ReplaceWithNamedCast("static_cast"); return; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 924b2c03cfd18..9ce04f2088f13 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -544,6 +544,10 @@ Changes in existing checks on Windows when the check was enabled with a 32-bit :program:`clang-tidy` binary. +- Improved :doc:`modernize-avoid-c-style-cast + ` by providing correct fixes + for C-style casts of nullptr. + - Improved :doc:`modernize-use-override ` by fixing an issue where the check would sometimes suggest inserting ``override`` in an invalid diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp index 52b4d471bda9b..9564833a8789a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp @@ -368,3 +368,12 @@ void functional_casts() { throw S2(5.0f); } +void f(int *); +void f(double *); + +void test_nullptr_cast() { + f((int*)nullptr); +} +// CHECK-MESSAGES: warning: C-style casts are discouraged +// CHECK-FIXES: f(static_cast(nullptr)); +