Fix #8938: FP identicalInnerCondition (#2471)

This commit is contained in:
Ken-Patrick Lehrmann 2020-01-04 11:38:56 +01:00 committed by Daniel Marjamäki
parent eca7ee9260
commit a9d423eef2
2 changed files with 23 additions and 0 deletions

View File

@ -627,6 +627,17 @@ void CheckCondition::multiCondition2()
if (nonConstFunctionCall)
continue;
std::vector<const Variable*> varsInCond;
visitAstNodes(condTok,
[&varsInCond](const Token *cond) {
if (cond->variable()) {
const Variable *var = cond->variable();
if(std::find(varsInCond.begin(), varsInCond.end(), var) == varsInCond.end())
varsInCond.push_back(var);
}
return ChildrenToVisit::op1_and_op2;
});
// parse until second condition is reached..
enum MULTICONDITIONTYPE { INNER, AFTER };
const Token *tok;
@ -710,6 +721,9 @@ void CheckCondition::multiCondition2()
}
}
}
if (Token::Match(tok, "%name% (") && isVariablesChanged(tok, tok->linkAt(1), true, varsInCond, mSettings, mTokenizer->isCPP())) {
break;
}
if (Token::Match(tok, "%type% (") && nonlocal && isNonConstFunctionCall(tok, mSettings->library)) // non const function call -> bailout if there are nonlocal variables
break;
if (Token::Match(tok, "case|break|continue|return|throw") && tok->scope() == endToken->scope())

View File

@ -1895,6 +1895,15 @@ private:
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
// #8938
check("void Delete(SS_CELLCOORD upperleft) {\n"
" if ((upperleft.Col == -1) && (upperleft.Row == -1)) {\n"
" GetActiveCell(&(upperleft.Col), &(upperleft.Row));\n"
" if (upperleft.Row == -1) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void oppositeInnerConditionPointers() {