Fixed #6095 (False positive oppositeInnerCondition - neglecting statements with side-effects)
This commit is contained in:
parent
9438b49bfb
commit
5c2a2a5c22
|
@ -426,6 +426,10 @@ void CheckCondition::oppositeInnerCondition()
|
||||||
}
|
}
|
||||||
if (Token::Match(tok->previous(), "++|--|& %var%"))
|
if (Token::Match(tok->previous(), "++|--|& %var%"))
|
||||||
break;
|
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% [,)]")) {
|
if (Token::Match(tok->previous(), "[(,] %var% [,)]")) {
|
||||||
// is variable unchanged? default is false..
|
// is variable unchanged? default is false..
|
||||||
bool unchanged = false;
|
bool unchanged = false;
|
||||||
|
|
|
@ -1062,6 +1062,37 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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
|
// #5731 - fp when undeclared variable is used
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" if (x == -1){\n"
|
" if (x == -1){\n"
|
||||||
|
|
Loading…
Reference in New Issue