Skip to content

[clang-tidy] modernize-use-nullptr matches "NULL" in templates #109169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

tkoeppe
Copy link
Contributor

@tkoeppe tkoeppe commented Sep 18, 2024

Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization.

Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed:

template <typename T>
struct X {
  T val;
  X() { val = NULL; }  // should diagnose
};

When the user says NULL, we expect that the destination type is always meant to be a pointer type, so this should be converted to nullptr. By contrast, we do not propose changing a literal 0 in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If NULL is used erroneously in such a situation, it should be changed to 0 or {}.)

@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: Thomas Köppe (tkoeppe)

Changes

Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization.

Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed:

template &lt;typename T&gt;
struct X {
  T val;
  X() { val = NULL; }  // should diagnose
};

When the user says NULL, we expect that the destination type is always meant to be a pointer type, so this should be converted to nullptr. By contrast, we do not propose changing a literal 0 in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If NULL is used erroneously in such a situation, it should be changed to 0 or {}.)


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

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp (+3-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp (+8)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
index 6a003a347badac..b2921690863b84 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) {
 StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef<StringRef> NameList) {
   auto ImplicitCastToNull = implicitCastExpr(
       anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)),
-      unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType()))),
+      anyOf(hasSourceExpression(gnuNullExpr()),
+            unless(hasImplicitDestinationType(
+                qualType(substTemplateTypeParmType())))),
       unless(hasSourceExpression(hasType(sugaredNullptrType()))),
       unless(hasImplicitDestinationType(
           qualType(matchers::matchesAnyListedTypeName(NameList)))));
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
index 7bc0925136aa86..1807d6bd56125b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp
@@ -84,6 +84,14 @@ void test_macro_expansion4() {
 #undef MY_NULL
 }
 
+template <typename T> struct pear {
+  T x;
+};
+void test_templated() {
+  pear<int*> p = { NULL };
+  dummy(p.x);
+}
+
 #define IS_EQ(x, y) if (x != y) return;
 void test_macro_args() {
   int i = 0;

@tkoeppe tkoeppe force-pushed the use-nullptr-template branch 2 times, most recently from 6e056a2 to d8a4676 Compare September 18, 2024 23:32
@EugeneZelenko
Copy link
Contributor

Please mention changes in Release Notes.

@tkoeppe tkoeppe force-pushed the use-nullptr-template branch from d8a4676 to 6077d49 Compare September 19, 2024 14:27
@tkoeppe
Copy link
Contributor Author

tkoeppe commented Sep 19, 2024

Done!

@tkoeppe tkoeppe force-pushed the use-nullptr-template branch from 6077d49 to 2f98433 Compare September 19, 2024 14:31
@tkoeppe tkoeppe force-pushed the use-nullptr-template branch from 2f98433 to 2b86607 Compare September 19, 2024 15:55
@tkoeppe tkoeppe force-pushed the use-nullptr-template branch 4 times, most recently from a7ea146 to d4e208f Compare September 19, 2024 18:00
@tkoeppe tkoeppe force-pushed the use-nullptr-template branch from d4e208f to 1149cba Compare September 19, 2024 20:44
@tkoeppe
Copy link
Contributor Author

tkoeppe commented Sep 20, 2024

@EugeneZelenko Could you please have another look?

@EugeneZelenko
Copy link
Contributor

I mostly check documentation and minor code issues. Please wait for at least one of active developers whom I added to reviewers list.

Copy link
Contributor

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

LGTM

…t not "0", when it appears on a substituted type of a template specialization.

Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed:

```c++
template <typename T>
struct X {
  T val;
  X() { val = NULL; }  // should diagnose
};
```

When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.)
@tkoeppe tkoeppe force-pushed the use-nullptr-template branch from 1149cba to 07df4ed Compare September 20, 2024 16:11
@tkoeppe
Copy link
Contributor Author

tkoeppe commented Sep 23, 2024

What happens next, do we need more approvals? @EugeneZelenko?

@EugeneZelenko
Copy link
Contributor

What happens next, do we need more approvals? @EugeneZelenko?

I think two approvals from active developers are enough.

@fmayer fmayer merged commit cebb7c0 into llvm:main Sep 25, 2024
9 checks passed
@tkoeppe tkoeppe deleted the use-nullptr-template branch September 25, 2024 16:27
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.

6 participants