Fixed #6095 (False positive oppositeInnerCondition - neglecting statements with side-effects)

This commit is contained in:
Daniel Marjamäki 2014-12-31 15:14:22 +01:00
parent 9438b49bfb
commit 5c2a2a5c22
2 changed files with 35 additions and 0 deletions

View File

@ -426,6 +426,10 @@ void CheckCondition::oppositeInnerCondition()
}
if (Token::Match(tok->previous(), "++|--|& %var%"))
break;
if (Token::Match(tok, "%var% . %var% (") &&
!tok->variable()->isConst() &&
!(tok->tokAt(2)->function() && tok->tokAt(2)->function()->isConst))
break;
if (Token::Match(tok->previous(), "[(,] %var% [,)]")) {
// is variable unchanged? default is false..
bool unchanged = false;

View File

@ -1062,6 +1062,37 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
{
// #6095 - calling member function that might change the state
check("void f() {\n"
" const Fred fred;\n" // <- fred is const, warn
" if (fred.isValid()) {\n"
" fred.dostuff();\n"
" if (!fred.isValid()) {}\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str());
check("class Fred { public: void dostuff() const; };\n"
"void f() {\n"
" Fred fred;\n"
" if (fred.isValid()) {\n"
" fred.dostuff();\n" // <- dostuff() is const, warn
" if (!fred.isValid()) {}\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:6]: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str());
check("void f() {\n"
" Fred fred;\n"
" if (fred.isValid()) {\n"
" fred.dostuff();\n"
" if (!fred.isValid()) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// #5731 - fp when undeclared variable is used
check("void f() {\n"
" if (x == -1){\n"