Skip to content

Commit a9d423e

Browse files
Ken-Patrickdanmar
authored andcommitted
Fix #8938: FP identicalInnerCondition (#2471)
1 parent eca7ee9 commit a9d423e

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/checkcondition.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ void CheckCondition::multiCondition2()
627627
if (nonConstFunctionCall)
628628
continue;
629629

630+
std::vector<const Variable*> varsInCond;
631+
visitAstNodes(condTok,
632+
[&varsInCond](const Token *cond) {
633+
if (cond->variable()) {
634+
const Variable *var = cond->variable();
635+
if(std::find(varsInCond.begin(), varsInCond.end(), var) == varsInCond.end())
636+
varsInCond.push_back(var);
637+
}
638+
return ChildrenToVisit::op1_and_op2;
639+
});
640+
630641
// parse until second condition is reached..
631642
enum MULTICONDITIONTYPE { INNER, AFTER };
632643
const Token *tok;
@@ -710,6 +721,9 @@ void CheckCondition::multiCondition2()
710721
}
711722
}
712723
}
724+
if (Token::Match(tok, "%name% (") && isVariablesChanged(tok, tok->linkAt(1), true, varsInCond, mSettings, mTokenizer->isCPP())) {
725+
break;
726+
}
713727
if (Token::Match(tok, "%type% (") && nonlocal && isNonConstFunctionCall(tok, mSettings->library)) // non const function call -> bailout if there are nonlocal variables
714728
break;
715729
if (Token::Match(tok, "case|break|continue|return|throw") && tok->scope() == endToken->scope())

test/testcondition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,15 @@ class TestCondition : public TestFixture {
18951895
" }\n"
18961896
"}");
18971897
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
1898+
1899+
// #8938
1900+
check("void Delete(SS_CELLCOORD upperleft) {\n"
1901+
" if ((upperleft.Col == -1) && (upperleft.Row == -1)) {\n"
1902+
" GetActiveCell(&(upperleft.Col), &(upperleft.Row));\n"
1903+
" if (upperleft.Row == -1) {}\n"
1904+
" }\n"
1905+
"}");
1906+
ASSERT_EQUALS("", errout.str());
18981907
}
18991908

19001909
void oppositeInnerConditionPointers() {

0 commit comments

Comments
 (0)