Fix #751 (if-condition considered redundant even with else-branch)

http://sourceforge.net/apps/trac/cppcheck/ticket/751
Fix also other issue with if(a.b) delete c.b;
Fix also other issue with if(a.b.c) delete a.b.c;
This commit is contained in:
Reijo Tomperi 2009-09-29 00:18:40 +03:00
parent 311f6dc92e
commit 64f0f3b308
2 changed files with 51 additions and 52 deletions

View File

@ -81,7 +81,7 @@ void CheckOther::warningRedundantCode()
if (! Token::simpleMatch(tok, "if (")) if (! Token::simpleMatch(tok, "if ("))
continue; continue;
const char *varname = NULL; std::string varname;
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->tokAt(2);
/* /*
@ -93,39 +93,18 @@ void CheckOther::warningRedundantCode()
* *
**/ **/
if (Token::Match(tok2, "%var% .|::")) while (Token::Match(tok2, "%var% .|::"))
{ {
varname.append(tok2->str());
varname.append(tok2->next()->str());
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
} }
if (Token::Match(tok2, "%var%")) if (!Token::Match(tok2, "%var% ) {"))
{
varname = tok2->strAt(0);
tok2 = tok2->next();
}
if (tok2->str() == ")")
{
tok2 = tok2->next();
}
else
{
varname = NULL;
}
if (varname == NULL)
continue; continue;
bool ifHasBracket = false; varname.append(tok2->str());
if (tok2->str() == "{") tok2 = tok2->tokAt(3);
{
tok2 = tok2->next();
ifHasBracket = true;
}
bool err = false;
bool funcHasBracket = false;
/* /*
* Possible constructions: * Possible constructions:
@ -142,6 +121,7 @@ void CheckOther::warningRedundantCode()
* *
**/ **/
bool funcHasBracket = false;
if (Token::Match(tok2, "free|kfree (")) if (Token::Match(tok2, "free|kfree ("))
{ {
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
@ -159,22 +139,25 @@ void CheckOther::warningRedundantCode()
} }
} }
if (Token::Match(tok2, "%var% ::|.")) std::string varname2;
while (Token::Match(tok2, "%var% ::|."))
{ {
varname2.append(tok2->str());
varname2.append(tok2->next()->str());
tok2 = tok2->tokAt(2); tok2 = tok2->tokAt(2);
} }
if (Token::Match(tok2, "%var%") && (strcmp(tok2->strAt(0), varname) == 0)) varname2.append(tok2->str());
{ if (Token::Match(tok2, "%var%") && varname == varname2)
tok2 = tok2->next(); tok2 = tok2->next();
err = true; else
} continue;
if (funcHasBracket) if (funcHasBracket)
{ {
if (tok2->str() != ")") if (tok2->str() != ")")
{ {
err = false; continue;
} }
else else
{ {
@ -182,27 +165,12 @@ void CheckOther::warningRedundantCode()
} }
} }
if (tok2->str() != ";") if (!Token::Match(tok2, "; } !!else"))
{ {
err = false; continue;
}
else
{
tok2 = tok2->next();
} }
if (ifHasBracket) redundantIfDelete0Error(tok);
{
if (tok2->str() != "}")
{
err = false;
}
}
if (err)
{
redundantIfDelete0Error(tok);
}
} }

View File

@ -214,6 +214,29 @@ private:
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" if (p)\n"
" delete p;\n"
" else\n"
" p = new P();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" if (0 != g->p)\n"
" delete this->p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" if (0 != this->g->a)\n"
" delete this->p->a;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void delete2() void delete2()
@ -255,6 +278,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n", errout.str());
check("void foo()\n"
"{\n"
" if (0 != this->p->a)\n"
" delete this->p->a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n", errout.str());
check("void Foo::deleteInstance()\n" check("void Foo::deleteInstance()\n"
"{\n" "{\n"
" if (Foo::instance != NULL)\n" " if (Foo::instance != NULL)\n"