This revision was automatically updated to reflect the committed changes. Closed by commit rL306091: [clang-tidy] Fix a false positive in modernize-use-nullptr. (authored by hokein).
Changed prior to commit: https://reviews.llvm.org/D34524?vs=103615&id=103702#toc Repository: rL LLVM https://reviews.llvm.org/D34524 Files: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp Index: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp @@ -200,12 +200,14 @@ return true; } - if (!FirstSubExpr) - FirstSubExpr = C->getSubExpr()->IgnoreParens(); - - // Ignore the expr if it is already a nullptr literal expr. - if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr)) + auto* CastSubExpr = C->getSubExpr()->IgnoreParens(); + // Ignore cast expressions which cast nullptr literal. + if (isa<CXXNullPtrLiteralExpr>(CastSubExpr)) { return true; + } + + if (!FirstSubExpr) + FirstSubExpr = CastSubExpr; if (C->getCastKind() != CK_NullToPointer && C->getCastKind() != CK_NullToMemberPointer) { Index: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp +++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp @@ -261,3 +261,17 @@ void IgnoreSubstTemplateType() { TemplateClass<int*> a(1); } + +// Test on casting nullptr. +struct G { + explicit G(bool, const char * = NULL) {} + // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use nullptr + // CHECK-FIXES: explicit G(bool, const char * = nullptr) {} +}; +bool g(const char*); +void test_cast_nullptr() { + G(g(nullptr)); + G(g((nullptr))); + G(g(static_cast<char*>(nullptr))); + G(g(static_cast<const char*>(nullptr))); +}
Index: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp @@ -200,12 +200,14 @@ return true; } - if (!FirstSubExpr) - FirstSubExpr = C->getSubExpr()->IgnoreParens(); - - // Ignore the expr if it is already a nullptr literal expr. - if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr)) + auto* CastSubExpr = C->getSubExpr()->IgnoreParens(); + // Ignore cast expressions which cast nullptr literal. + if (isa<CXXNullPtrLiteralExpr>(CastSubExpr)) { return true; + } + + if (!FirstSubExpr) + FirstSubExpr = CastSubExpr; if (C->getCastKind() != CK_NullToPointer && C->getCastKind() != CK_NullToMemberPointer) { Index: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp +++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp @@ -261,3 +261,17 @@ void IgnoreSubstTemplateType() { TemplateClass<int*> a(1); } + +// Test on casting nullptr. +struct G { + explicit G(bool, const char * = NULL) {} + // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use nullptr + // CHECK-FIXES: explicit G(bool, const char * = nullptr) {} +}; +bool g(const char*); +void test_cast_nullptr() { + G(g(nullptr)); + G(g((nullptr))); + G(g(static_cast<char*>(nullptr))); + G(g(static_cast<const char*>(nullptr))); +}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits