Part of fix for ticket #284 (style check: redundant condition improvement)

Fixed case "if (p != NULL) delete p;" and also added test case for it.

http://apps.sourceforge.net/trac/cppcheck/ticket/284
This commit is contained in:
Slava Semushin 2009-05-20 01:23:43 +07:00
parent 1e2545439e
commit bc62472a18
2 changed files with 8 additions and 1 deletions

View File

@ -82,7 +82,7 @@ void CheckOther::WarningRedundantCode()
varname1 = tok->strAt(2); varname1 = tok->strAt(2);
tok2 = tok->tokAt(4); tok2 = tok->tokAt(4);
} }
else if (Token::Match(tok, "if ( %var% != NULL )")) else if (Token::Match(tok, "if ( %var% != 0 )"))
{ {
varname1 = tok->strAt(2); varname1 = tok->strAt(2);
tok2 = tok->tokAt(6); tok2 = tok->tokAt(6);

View File

@ -145,6 +145,13 @@ private:
" delete p;\n" " delete p;\n"
"}\n"); "}\n");
ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str());
check("void foo()\n"
"{\n"
" if (p != NULL)\n"
" delete p;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str());
} }
void unreachable1() void unreachable1()