diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index b8025d72c..91bef73fb 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -207,6 +207,10 @@ void CheckNullPointer::nullPointerAfterLoop() if (varid == 0) continue; + // Is variable a pointer? + if (!isPointer(varid)) + continue; + // Get variable name for the loop variable const std::string varname(tok->strAt(2)); @@ -229,9 +233,7 @@ void CheckNullPointer::nullPointerAfterLoop() // Is the loop variable dereferenced? if (CheckNullPointer::isPointerDeRef(tok2, unknown)) { - // Is loop variable a pointer? - if (isPointer(varid)) - nullPointerError(tok2, varname); + nullPointerError(tok2, varname, tok->linenr()); } break; } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 619b40a2c..4fc97ead9 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -78,7 +78,7 @@ private: " while (tok);\n" " tok = tok->next();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: tok\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", errout.str()); check("void foo()\n" "{\n"