From 76f759fcc417a13e188f463a98890d6495edcdef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 15 Jan 2021 20:48:07 +0100 Subject: [PATCH] Fixed #9702 (False positive: Opposite inner condition when modifying member variable used by inner condition) --- lib/checkcondition.cpp | 5 ++++- test/testcondition.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 3605b67cc..e15a6c74c 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -594,12 +594,14 @@ void CheckCondition::multiCondition2() if (!Token::simpleMatch(scope.classDef->linkAt(1), ") {")) continue; + bool functionCall = false; bool nonConstFunctionCall = false; bool nonlocal = false; // nonlocal variable used in condition std::set vars; // variables used in condition visitAstNodes(condTok, [&](const Token *cond) { if (Token::Match(cond, "%name% (")) { + functionCall = true; nonConstFunctionCall = isNonConstFunctionCall(cond, mSettings->library); if (nonConstFunctionCall) return ChildrenToVisit::done; @@ -755,7 +757,8 @@ void CheckCondition::multiCondition2() break; } 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%|++|--")) break; if (Token::Match(tok->astParent(), "*|.|[")) { diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 02f844c29..d4beb476c 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1921,6 +1921,19 @@ private: " }\n" "}"); 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() {