Skip to content

Commit 6e056a2

Browse files
committed
[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 `{}`.)
1 parent 644899a commit 6e056a2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) {
3838
StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef<StringRef> NameList) {
3939
auto ImplicitCastToNull = implicitCastExpr(
4040
anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)),
41-
unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType()))),
41+
anyOf(hasSourceExpression(gnuNullExpr()),
42+
unless(hasImplicitDestinationType(
43+
qualType(substTemplateTypeParmType())))),
4244
unless(hasSourceExpression(hasType(sugaredNullptrType()))),
4345
unless(hasImplicitDestinationType(
4446
qualType(matchers::matchesAnyListedTypeName(NameList)))));

clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ void test_macro_expansion4() {
8484
#undef MY_NULL
8585
}
8686

87+
template <typename T> struct pear {
88+
pear() { x = __null; }
89+
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr [modernize-use-nullptr]
90+
// CHECK-FIXES: x = nullptr;
91+
T x;
92+
};
93+
void test_templated() {
94+
pear<int*> p;
95+
dummy(p.x);
96+
}
97+
8798
#define IS_EQ(x, y) if (x != y) return;
8899
void test_macro_args() {
89100
int i = 0;

0 commit comments

Comments
 (0)