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

Fixed case "if (p) 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:28:25 +07:00
parent bc62472a18
commit f5300ae56c
2 changed files with 9 additions and 2 deletions

View File

@ -102,7 +102,7 @@ void CheckOther::WarningRedundantCode()
}
else if (Token::Match(tok2, "delete [ ] %var% ; }"))
{
err = (strcmp(tok2->strAt(1), varname1) == 0);
err = (strcmp(tok2->strAt(3), varname1) == 0);
}
else if (Token::Match(tok2, "free ( %var% ) ; }"))
{
@ -121,7 +121,7 @@ void CheckOther::WarningRedundantCode()
}
else if (Token::Match(tok2, "delete [ ] %var% ;"))
{
err = (strcmp(tok2->strAt(1), varname1) == 0);
err = (strcmp(tok2->strAt(3), varname1) == 0);
}
else if (Token::Match(tok2, "free ( %var% ) ;"))
{

View File

@ -152,6 +152,13 @@ private:
" 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());
check("void foo()\n"
"{\n"
" if (p)\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()