This commit is contained in:
Reijo Tomperi 2008-12-20 22:24:11 +00:00
parent a0cee01d35
commit 7c3fbd7060
2 changed files with 61 additions and 11 deletions

View File

@ -98,18 +98,47 @@ void CheckOther::WarningRedundantCode()
if (varname1==NULL || tok2==NULL)
continue;
bool err = false;
if ( tok2->str() == "{" )
{
tok2 = tok2->next();
bool err = false;
if (TOKEN::Match(tok2,"delete %var% ;"))
err = (strcmp(tok2->strAt(1),varname1)==0);
else if (TOKEN::Match(tok2,"delete [ ] %var% ;"))
err = (strcmp(tok2->strAt(1),varname1)==0);
else if (TOKEN::Match(tok2,"free ( %var% )"))
err = (strcmp(tok2->strAt(2),varname1)==0);
else if (TOKEN::Match(tok2,"kfree ( %var% )"))
err = (strcmp(tok2->strAt(2),varname1)==0);
if (TOKEN::Match(tok2,"delete %var% ; }"))
{
err = (strcmp(tok2->strAt(1),varname1)==0);
}
else if (TOKEN::Match(tok2,"delete [ ] %var% ; }"))
{
err = (strcmp(tok2->strAt(1),varname1)==0);
}
else if (TOKEN::Match(tok2,"free ( %var% ) ; }"))
{
err = (strcmp(tok2->strAt(2),varname1)==0);
}
else if (TOKEN::Match(tok2,"kfree ( %var% ) ; }"))
{
err = (strcmp(tok2->strAt(2),varname1)==0);
}
}
else
{
if (TOKEN::Match(tok2,"delete %var% ;"))
{
err = (strcmp(tok2->strAt(1),varname1)==0);
}
else if (TOKEN::Match(tok2,"delete [ ] %var% ;"))
{
err = (strcmp(tok2->strAt(1),varname1)==0);
}
else if (TOKEN::Match(tok2,"free ( %var% ) ;"))
{
err = (strcmp(tok2->strAt(2),varname1)==0);
}
else if (TOKEN::Match(tok2,"kfree ( %var% ) ;"))
{
err = (strcmp(tok2->strAt(2),varname1)==0);
}
}
if (err)
{

View File

@ -34,7 +34,9 @@ private:
void run()
{
// TODO TEST_CASE( delete1 );
TEST_CASE( delete1 );
TEST_CASE( delete2 );
}
void check( const char code[] )
@ -59,11 +61,30 @@ private:
" if (p)\n"
" {\n"
" delete p;\n"
" abc = 123;\n"
" p = 0;\n"
" }\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}
void delete2()
{
check( "void foo()\n"
"{\n"
" if (p)\n"
" {\n"
" delete p;\n"
" }\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:3]: 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]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str() );
}
};
REGISTER_TEST( TestOther )