diff --git a/src/checkother.cpp b/src/checkother.cpp index cb50d8bc7..72c5e6d58 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1084,7 +1084,11 @@ void CheckOther::nullPointer() if (varid == 0) continue; - for (const Token *tok1 = tok->previous(); tok1; tok1 = tok1->previous()) + const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid); + if (!Token::Match(decltok->tokAt(-3), "[;,(] %var% *")) + continue; + + for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous()) { if (tok1->varId() == varid) { diff --git a/test/testother.cpp b/test/testother.cpp index 65c60b837..82a4d5950 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -565,6 +565,14 @@ private: " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkNullPointer("void foo(int x)\n" + "{\n" + " int a = 2 * x;" + " if (x == 0)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); }