Skip to content

Conversation

@SonareMradul
Copy link

@SonareMradul SonareMradul commented Dec 20, 2025

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

Fixes #173147

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2025

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Mradul Sonare (SonareMradul)

Changes

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

Fixes #<173147>


Full diff: https://github.com/llvm/llvm-project/pull/173159.diff

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp (+5)
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;

@github-actions
Copy link

github-actions bot commented Dec 20, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp
index 2e7fc0f36..6c49869cf 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidCStyleCastCheck.cpp
@@ -218,10 +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 (isa<CXXNullPtrLiteralExpr>(
+            CastExpr->getSubExprAsWritten()->IgnoreImpCasts())) {
+      ReplaceWithNamedCast("static_cast");
+      return;
+    }
 
     if (FnToFnCast) {
       ReplaceWithNamedCast("static_cast");

@localspook
Copy link
Contributor

The change looks good, but please:

  • add a release note in clang-tools-extra/docs/ReleaseNotes.rst
  • add tests for the change in clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-style-cast.cpp
  • edit your PR message to change Fixes #<173147> to Fixes #173147 (that way, when this PR is merged, Github will close that issue automatically)

on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
binary.

- Fixed google-readability-casting to provide a fix-it for C-style casts of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the existing style of documenting changes:

- - Fixed google-readability-casting to provide a fix-it for C-style casts of
-   nullptr.
+ - Improved :doc:`modernize-avoid-c-style-cast
+   <clang-tidy/checks/modernize/avoid-c-style-cast>` by providing correct fixes 
+   for C-style casts of nullptr

ReplaceWithNamedCast("static_cast");
return;
case CK_NoOp:
if (isa<CXXNullPtrLiteralExpr>(CastExpr->getSubExprAsWritten()->IgnoreImpCasts())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a formatting issue? You can run git clang-format HEAD~1 to fix it :)

@github-actions
Copy link

github-actions bot commented Dec 21, 2025

🐧 Linux x64 Test Results

  • 3057 tests passed
  • 7 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

Clang Tools

Clang Tools.clang-tidy/infrastructure/alphabetical-order.test
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# executed command: /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# note: command had no output on stdout or stderr
# RUN: at line 2
diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/clang-tidy/checks/list.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# executed command: diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/clang-tidy/checks/list.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# note: command had no output on stdout or stderr
# RUN: at line 4
/usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst
# executed command: /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst
# .---command stderr------------
# | 
# | Entries in 'clang-tools-extra/docs/ReleaseNotes.rst' are not alphabetically sorted.
# | Fix the ordering by applying diff printed below.
# | 
# `-----------------------------
# RUN: at line 5
diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst
# executed command: diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst
# .---command stdout------------
# | *** /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst
# | --- /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst
# | ***************
# | *** 521,526 ****
# | --- 521,530 ----
# |     <clang-tidy/checks/modernize/avoid-c-arrays>` to not diagnose array types
# |     which are part of an implicit instantiation of a template.
# |   
# | + - Improved :doc:`modernize-avoid-c-style-cast  
# | +   <clang-tidy/checks/modernize/avoid-c-style-cast>` by providing correct fixes 
# | +   for C-style casts of nullptr.
# | +   
# |   - Improved :doc:`modernize-use-constraints
# |     <clang-tidy/checks/modernize/use-constraints>` check by fixing a crash on
# |     uses of non-standard ``enable_if`` with a signature different from
# | ***************
# | *** 544,553 ****
# |     on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
# |     binary.
# |   
# | - - Improved :doc:`modernize-avoid-c-style-cast  
# | -   <clang-tidy/checks/modernize/avoid-c-style-cast>` by providing correct fixes 
# | -   for C-style casts of nullptr.
# | -   
# |   - Improved :doc:`modernize-use-override
# |     <clang-tidy/checks/modernize/use-override>` by fixing an issue where
# |     the check would sometimes suggest inserting ``override`` in an invalid
# | --- 548,553 ----
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@github-actions
Copy link

github-actions bot commented Dec 21, 2025

🪟 Windows x64 Test Results

  • 2995 tests passed
  • 30 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

Clang Tools

Clang Tools.clang-tidy/infrastructure/alphabetical-order.test
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
C:/Python312/python.exe C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# executed command: C:/Python312/python.exe 'C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py' -o 'C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.clang-tidy-checks-list.rst'
# note: command had no output on stdout or stderr
# RUN: at line 2
diff --strip-trailing-cr C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../docs/clang-tidy/checks/list.rst C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.clang-tidy-checks-list.rst
# executed command: diff --strip-trailing-cr 'C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../docs/clang-tidy/checks/list.rst' 'C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.clang-tidy-checks-list.rst'
# note: command had no output on stdout or stderr
# RUN: at line 4
C:/Python312/python.exe C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.ReleaseNotes.rst
# executed command: C:/Python312/python.exe 'C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py' -o 'C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.ReleaseNotes.rst'
# .---command stderr------------
# | 
# | Entries in 'clang-tools-extra/docs/ReleaseNotes.rst' are not alphabetically sorted.
# | Fix the ordering by applying diff printed below.
# | 
# `-----------------------------
# RUN: at line 5
diff --strip-trailing-cr C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../docs/ReleaseNotes.rst C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.ReleaseNotes.rst
# executed command: diff --strip-trailing-cr 'C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../docs/ReleaseNotes.rst' 'C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.ReleaseNotes.rst'
# .---command stdout------------
# | *** C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\infrastructure/../../../docs/ReleaseNotes.rst
# | --- C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\infrastructure\Output\alphabetical-order.test.tmp.ReleaseNotes.rst
# | ***************
# | *** 521,526 ****
# | --- 521,530 ----
# |     <clang-tidy/checks/modernize/avoid-c-arrays>` to not diagnose array types
# |     which are part of an implicit instantiation of a template.
# |   
# | + - Improved :doc:`modernize-avoid-c-style-cast  
# | +   <clang-tidy/checks/modernize/avoid-c-style-cast>` by providing correct fixes 
# | +   for C-style casts of nullptr.
# | +   
# |   - Improved :doc:`modernize-use-constraints
# |     <clang-tidy/checks/modernize/use-constraints>` check by fixing a crash on
# |     uses of non-standard ``enable_if`` with a signature different from
# | ***************
# | *** 544,553 ****
# |     on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
# |     binary.
# |   
# | - - Improved :doc:`modernize-avoid-c-style-cast  
# | -   <clang-tidy/checks/modernize/avoid-c-style-cast>` by providing correct fixes 
# | -   for C-style casts of nullptr.
# | -   
# |   - Improved :doc:`modernize-use-override
# |     <clang-tidy/checks/modernize/use-override>` by fixing an issue where
# |     the check would sometimes suggest inserting ``override`` in an invalid
# | --- 548,553 ----
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@SonareMradul
Copy link
Author

@zeyi2 Could you please advise what is required from my side to get the Linux and Windows checks to paas?

on Windows when the check was enabled with a 32-bit :program:`clang-tidy`
binary.

- Improved :doc:`modernize-avoid-c-style-cast
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Clang-Tidy documentations, we want entries in Changes in existing checks to keep alphabetical order, so this new entries should be placed between modernize-avoid-c-arrays and modernize-use-constraints :)

@zeyi2
Copy link
Member

zeyi2 commented Dec 21, 2025

what is required from my side to get the Linux and Windows checks to paas

You can try building check-clang-tools on your local machine to verify the test cases pass (though it will take some time). Since CI runs need to be manually approved for new contributors, local testing is usually faster for debugging.

If you notice any failures on GitHub, the specific output will be available in the comments to help you troubleshoot.

}
// CHECK-MESSAGES: warning: C-style casts are discouraged
// CHECK-FIXES: f(static_cast<int*>(nullptr));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excessive newline.


throw S2(5.0f);
}
void f(int *);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be good idea to separate with newline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clang-tidy: google-readability-casting, aka modernize-avoid-c-style-cast, for nullptr

5 participants