Fixed #5750 (FP:Opposite conditions in nested 'if' blocks lead to a dead code block)

This commit is contained in:
Daniel Marjamäki 2014-05-06 16:15:12 +02:00
parent 2d2847ddbd
commit b354de6b23
2 changed files with 19 additions and 3 deletions

View File

@ -148,8 +148,8 @@ static bool isOppositeCond(const Token * const cond1, const Token * const cond2,
if (isSameExpression(cond1->astOperand1(), cond2->astOperand1(), constFunctions) && if (isSameExpression(cond1->astOperand1(), cond2->astOperand1(), constFunctions) &&
isSameExpression(cond1->astOperand2(), cond2->astOperand2(), constFunctions)) { isSameExpression(cond1->astOperand2(), cond2->astOperand2(), constFunctions)) {
comp2 = cond2->str(); comp2 = cond2->str();
} else if (isSameExpression(cond1->astOperand1(), cond2->astOperand1(), constFunctions) && } else if (isSameExpression(cond1->astOperand1(), cond2->astOperand2(), constFunctions) &&
isSameExpression(cond1->astOperand2(), cond2->astOperand2(), constFunctions)) { isSameExpression(cond1->astOperand2(), cond2->astOperand1(), constFunctions)) {
comp2 = cond2->str(); comp2 = cond2->str();
if (comp2[0] == '>') if (comp2[0] == '>')
comp2[0] = '<'; comp2[0] = '<';
@ -3342,7 +3342,8 @@ void CheckOther::oppositeInnerCondition()
} }
if (Token::Match(tok,"%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables if (Token::Match(tok,"%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables
break; break;
else if (tok->varId() && vars.find(tok->varId()) != vars.end()) { else if ((tok->varId() && vars.find(tok->varId()) != vars.end()) ||
(!tok->varId() && nonlocal)) {
if (Token::Match(tok, "%var% ++|--|=")) if (Token::Match(tok, "%var% ++|--|="))
break; break;
if (Token::Match(tok->previous(), "++|--|& %var%")) if (Token::Match(tok->previous(), "++|--|& %var%"))

View File

@ -302,6 +302,13 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str());
check("void foo(int a, int b) {\n"
" if(a==b)\n"
" if(b!=a)\n"
" cout << a;\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str());
check("void foo(int a) {\n" check("void foo(int a) {\n"
" if(a >= 50) {\n" " if(a >= 50) {\n"
" if(a < 50)\n" " if(a < 50)\n"
@ -402,6 +409,14 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #5750 - another fp when undeclared variable is used
check("void f() {\n"
" if (r < w){\n"
" r += 3;\n"
" if (r > w) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void emptyBrackets() { void emptyBrackets() {