diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 0214eceedd1..0232ffcedf3 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -310,7 +310,8 @@ void CheckNullPointer::nullPointerByDeRefAndCheck() return true; }; - std::vector tokens = findTokensSkipDeadAndUnevaluatedCode(mSettings->library, scope->bodyStart, scope->bodyEnd, pred); + const Token* const start = (scope->function && scope->function->isConstructor()) ? scope->function->token : scope->bodyStart; // Check initialization list + std::vector tokens = findTokensSkipDeadAndUnevaluatedCode(mSettings->library, start, scope->bodyEnd, pred); for (const Token *tok : tokens) { const ValueFlow::Value *value = tok->getValue(0); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index ee971fd44a8..501476ceb45 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3742,6 +3742,15 @@ class TestNullPointer : public TestFixture { " return 1 ? 1 : *(int*)0 = 1;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check ("struct S {\n" // #13220 + " explicit S(int* p) : i(*p) {\n" + " if (p) {}\n" + " }\n" + " int i;\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:2:29]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n", + errout_str()); } void nullpointerDelete() {