[clang-tools-extra] [clang-tidy] Add fix-it for C-style cast of nullptr (PR #173159)

Mradul Sonare via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 20 09:37:14 PST 2025


https://github.com/SonareMradul created https://github.com/llvm/llvm-project/pull/173159

Adds a fix-it for C-style casts of nullptr in google-readability-casting,
suggesting static_cast<T*>(nullptr).

Fixes #<173147>


>From c44ca1e6ac8f2fa11abf44de0d9ad7afabaddaab Mon Sep 17 00:00:00 2001
From: "tashutech410 at gmail.com" <tashutech410 at gmail.com>
Date: Sat, 20 Dec 2025 17:31:59 +0000
Subject: [PATCH] [clang-tidy] Add fix-it for C-style cast of nullptr

---
 .../clang-tidy/modernize/AvoidCStyleCastCheck.cpp            | 5 +++++
 1 file changed, 5 insertions(+)

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<CXXNullPtrLiteralExpr>(CastExpr->getSubExprAsWritten()->IgnoreImpCasts())) {
+  ReplaceWithNamedCast("static_cast");
+  return;
+}
+
     if (FnToFnCast) {
       ReplaceWithNamedCast("static_cast");
       return;



More information about the cfe-commits mailing list