diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6d984f591..ba2fadd0d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -932,6 +932,23 @@ static bool valueFlowForward(Token * const startToken, return false; } + // if known variable is changed in loop body, change it to a possible value.. + if (tok2->str() != "if") { + bool isChanged = false; + for (std::list::iterator it = values.begin(); it != values.end(); ++it) { + if (it->valueKind == ValueFlow::Value::Known) { + if (!isChanged) { + const Token *bodyStart = tok2->linkAt(1)->next(); + if (!isVariableChanged(bodyStart, bodyStart->link(), varid)) + break; + isChanged = true; + } + + it->valueKind = ValueFlow::Value::Possible; + } + } + } + // Set values in condition for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) { if (tok3->varId() == varid) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 83ce94876..6aad642e6 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1566,6 +1566,16 @@ private: } ASSERT_EQUALS(true, possible); + code = "void f() {\n" + " int x = 0;\n" + " while (!x) {\n" // <- possible value + " x = dostuff();\n" + " }\n" + "}"; + value = valueOfTok(code, "!"); + ASSERT_EQUALS(1, value.intvalue); + ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind); + // after condition code = "int f(int x) {\n" " if (x == 4) {}\n"