diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 1ac0a22d7..545af92e5 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -678,8 +678,12 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) { + bool dummy = false; if (Token::Match(tok2, "return * %varid%", varid)) - nullPointerError(tok2, tok->strAt(3), linenr); + nullPointerError(tok2, pointerName, linenr); + else if (Token::Match(tok2, "return %varid%", varid) && + CheckNullPointer::isPointerDeRef(tok2->next(), dummy)) + nullPointerError(tok2, pointerName, linenr); break; } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 316dbff1b..005c25657 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1148,6 +1148,12 @@ private: " len = decltype(*pFoo);\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("int foo(struct Fred *fred) {\n" + " if (fred) { int a = 0; }\n" + " return fred->a;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); } // Test CheckNullPointer::nullConstantDereference