Fixed bug with redundant condition: http://sourceforge.net/forum/forum.php?thread_id=2711792&forum_id=693501
This commit is contained in:
parent
a0cee01d35
commit
7c3fbd7060
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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 )
|
||||
|
|
Loading…
Reference in New Issue