diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ab02389b6..33bcd3de6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -148,8 +148,8 @@ static bool isOppositeCond(const Token * const cond1, const Token * const cond2, if (isSameExpression(cond1->astOperand1(), cond2->astOperand1(), constFunctions) && isSameExpression(cond1->astOperand2(), cond2->astOperand2(), constFunctions)) { comp2 = cond2->str(); - } else if (isSameExpression(cond1->astOperand1(), cond2->astOperand1(), constFunctions) && - isSameExpression(cond1->astOperand2(), cond2->astOperand2(), constFunctions)) { + } else if (isSameExpression(cond1->astOperand1(), cond2->astOperand2(), constFunctions) && + isSameExpression(cond1->astOperand2(), cond2->astOperand1(), constFunctions)) { comp2 = cond2->str(); if (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 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% ++|--|=")) break; if (Token::Match(tok->previous(), "++|--|& %var%")) diff --git a/test/testother.cpp b/test/testother.cpp index a5ca3d8be..398571629 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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()); + 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" " if(a >= 50) {\n" " if(a < 50)\n" @@ -402,6 +409,14 @@ private: "}"); 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() {