diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index cc5d9cda2..9a0687e1f 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -461,7 +461,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (Token::Match(tok2, "goto|return|continue|break|throw|if")) { - if (Token::Match(tok2, "return * %var%")) + if (Token::Match(tok2, "return * %varid%", varid)) nullPointerError(tok2, tok->strAt(3)); break; } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 69357d396..b1b3295eb 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -43,7 +43,7 @@ private: TEST_CASE(nullpointer7); TEST_CASE(nullpointer8); TEST_CASE(nullpointer9); - TEST_CASE(nullpointer10); // check if pointer is null and then dereference it + TEST_CASE(checkAndDeRef); // check if pointer is null and then dereference it } void check(const char code[]) @@ -631,7 +631,7 @@ private: } // Check if pointer is null and the dereference it - void nullpointer10() + void checkAndDeRef() { check("void foo(char *p) {\n" " if (!p) {\n" @@ -671,6 +671,13 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void foo(bool p) {\n" + " if (!p) {\n" + " }\n" + " return *x;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // This is why this check can't be used on the simplified token list check("void f(Foo *foo) {\n" " if (!dynamic_cast(foo)) {\n"