Index: clang/lib/AST/Expr.cpp =================================================================== --- clang/lib/AST/Expr.cpp +++ clang/lib/AST/Expr.cpp @@ -3812,6 +3812,11 @@ return Source->isNullPointerConstant(Ctx, NPC); } + // If the expression has no type information, it cannot be a null pointer + // constant. + if (getType().isNull()) + return NPCK_NotNull; + // C++11 nullptr_t is always a null pointer constant. if (getType()->isNullPtrType()) return NPCK_CXX11_nullptr; Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2608,6 +2608,14 @@ EXPECT_TRUE(matches("char *cp = (char *)0;", expr(nullPointerConstant()))); EXPECT_TRUE(matches("int *ip = 0;", expr(nullPointerConstant()))); EXPECT_TRUE(matches("int i = 0;", expr(nullPointerConstant()))); + const char kTest[] = R"( + template + struct MyTemplate { + MyTemplate() : field_(__null) {} + T* field_; + }; + )"; + EXPECT_TRUE(matches(kTest, expr(nullPointerConstant()))); } TEST(HasExternalFormalLinkage, Basic) {