[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
Thomas Köppe via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 18 10:08:22 PDT 2024
https://github.com/tkoeppe created https://github.com/llvm/llvm-project/pull/109169
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:
```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 `{}`.)
>From d6a67729477b178d2439dfd31539a82ab7041e10 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= <tkoeppe at google.com>
Date: Wed, 18 Sep 2024 17:04:44 +0000
Subject: [PATCH] [clang-tidy] 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:
```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 `{}`.)
---
.../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++-
.../test/clang-tidy/checkers/modernize/use-nullptr.cpp | 8 ++++++++
2 files changed, 11 insertions(+), 1 deletion(-)
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;
More information about the cfe-commits
mailing list