diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 030883be4..ed592e0fb 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -509,7 +509,10 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog } if (tok2->str() == "}") { - if (Token::findmatch(tok2->link(), "%varid%", tok2, varid)) { + const Token *vartok = Token::findmatch(tok2->link(), "%varid%", tok2, varid); + while (Token::Match(vartok, "%var% = %num% ;") && !vartok->tokAt(2)->getValue(num)) + vartok = Token::findmatch(vartok->next(), "%varid%", tok2, varid); + if (vartok) { if (settings->debugwarnings) { std::string errmsg = "variable "; if (var) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 657d056d2..d1ade8878 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -430,6 +430,13 @@ private: " if (x == 123) {}\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (debug) ValueFlow bailout: variable x stopping on }\n", errout.str()); + + code = "void f(int x) {\n" + " a = x;\n" + " if (abc) { x = 1; }\n" // <- condition must be false if x is 7 in next line + " if (x == 7) { }\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 2U, 7)); } void valueFlowBeforeConditionGlobalVariables() {