Skip to content

Commit 76f759f

Browse files
committed
Fixed #9702 (False positive: Opposite inner condition when modifying member variable used by inner condition)
1 parent 583ee7b commit 76f759f

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/checkcondition.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,14 @@ void CheckCondition::multiCondition2()
594594
if (!Token::simpleMatch(scope.classDef->linkAt(1), ") {"))
595595
continue;
596596

597+
bool functionCall = false;
597598
bool nonConstFunctionCall = false;
598599
bool nonlocal = false; // nonlocal variable used in condition
599600
std::set<int> vars; // variables used in condition
600601
visitAstNodes(condTok,
601602
[&](const Token *cond) {
602603
if (Token::Match(cond, "%name% (")) {
604+
functionCall = true;
603605
nonConstFunctionCall = isNonConstFunctionCall(cond, mSettings->library);
604606
if (nonConstFunctionCall)
605607
return ChildrenToVisit::done;
@@ -755,7 +757,8 @@ void CheckCondition::multiCondition2()
755757
break;
756758
}
757759
if ((tok->varId() && vars.find(tok->varId()) != vars.end()) ||
758-
(!tok->varId() && nonlocal)) {
760+
(!tok->varId() && nonlocal) ||
761+
(functionCall && tok->variable() && !tok->variable()->isLocal())) {
759762
if (Token::Match(tok, "%name% %assign%|++|--"))
760763
break;
761764
if (Token::Match(tok->astParent(), "*|.|[")) {

test/testcondition.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,19 @@ class TestCondition : public TestFixture {
19211921
" }\n"
19221922
"}");
19231923
ASSERT_EQUALS("", errout.str());
1924+
1925+
// #9702
1926+
check("struct A {\n"
1927+
" void DoTest() {\n"
1928+
" if (!IsSet()) {\n"
1929+
" m_value = true;\n"
1930+
" if (IsSet());\n"
1931+
" }\n"
1932+
" }\n"
1933+
" bool IsSet() const { return m_value; }\n"
1934+
" bool m_value = false;\n"
1935+
"};");
1936+
ASSERT_EQUALS("", errout.str());
19241937
}
19251938

19261939
void oppositeInnerConditionPointers() {

0 commit comments

Comments
 (0)