Fixed #9702 (False positive: Opposite inner condition when modifying member variable used by inner condition)

This commit is contained in:
Daniel Marjamäki 2021-01-15 20:48:07 +01:00
parent 583ee7b70e
commit 76f759fcc4
2 changed files with 17 additions and 1 deletions

View File

@ -594,12 +594,14 @@ void CheckCondition::multiCondition2()
if (!Token::simpleMatch(scope.classDef->linkAt(1), ") {")) if (!Token::simpleMatch(scope.classDef->linkAt(1), ") {"))
continue; continue;
bool functionCall = false;
bool nonConstFunctionCall = false; bool nonConstFunctionCall = false;
bool nonlocal = false; // nonlocal variable used in condition bool nonlocal = false; // nonlocal variable used in condition
std::set<int> vars; // variables used in condition std::set<int> vars; // variables used in condition
visitAstNodes(condTok, visitAstNodes(condTok,
[&](const Token *cond) { [&](const Token *cond) {
if (Token::Match(cond, "%name% (")) { if (Token::Match(cond, "%name% (")) {
functionCall = true;
nonConstFunctionCall = isNonConstFunctionCall(cond, mSettings->library); nonConstFunctionCall = isNonConstFunctionCall(cond, mSettings->library);
if (nonConstFunctionCall) if (nonConstFunctionCall)
return ChildrenToVisit::done; return ChildrenToVisit::done;
@ -755,7 +757,8 @@ void CheckCondition::multiCondition2()
break; break;
} }
if ((tok->varId() && vars.find(tok->varId()) != vars.end()) || if ((tok->varId() && vars.find(tok->varId()) != vars.end()) ||
(!tok->varId() && nonlocal)) { (!tok->varId() && nonlocal) ||
(functionCall && tok->variable() && !tok->variable()->isLocal())) {
if (Token::Match(tok, "%name% %assign%|++|--")) if (Token::Match(tok, "%name% %assign%|++|--"))
break; break;
if (Token::Match(tok->astParent(), "*|.|[")) { if (Token::Match(tok->astParent(), "*|.|[")) {

View File

@ -1921,6 +1921,19 @@ private:
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #9702
check("struct A {\n"
" void DoTest() {\n"
" if (!IsSet()) {\n"
" m_value = true;\n"
" if (IsSet());\n"
" }\n"
" }\n"
" bool IsSet() const { return m_value; }\n"
" bool m_value = false;\n"
"};");
ASSERT_EQUALS("", errout.str());
} }
void oppositeInnerConditionPointers() { void oppositeInnerConditionPointers() {