diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 8dca3e7c0..a2e5abe62 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -516,9 +516,13 @@ void CheckNullPointer::nullPointerByDeRefAndChec() for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous()) { - if (tok1->str() == ")" && Token::Match(tok1->link()->tokAt(-3), "%varid% = %var%", varid)) + if (tok1->str() == ")" && Token::Match(tok1->link()->previous(), "%var% (")) { - break; + const Token *tok2 = tok1->link(); + while (tok2 && !Token::Match(tok2, "[;{}]")) + tok2 = tok2->previous(); + if (Token::Match(tok2, "[;{}] %varid% = %var%", varid)) + break; } if (tok1->str() == ")" && Token::Match(tok1->link()->previous(), "while ( %varid%", varid)) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 764879d65..4f0eb41c3 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -487,6 +487,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo(x *p)\n" + "{\n" + " p = aa->bar(p->next);\n" + " if (!p)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("void foo(struct ABC *abc)\n" "{\n" " abc = abc ? abc->next : 0;\n"