diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 9215f9338..359974564 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1318,8 +1318,17 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat if (!tok->astOperand2() || tok->astOperand2()->values.empty()) continue; - const std::list& values = tok->astOperand2()->values; + std::list values = tok->astOperand2()->values; const bool constValue = tok->astOperand2()->isNumber(); + + // Static variable initialisation? + if (var->isStatic() && var->nameToken() == tok->astOperand1()) { + for (std::list::iterator it = values.begin(); it != values.end(); ++it) { + if (it->valueKind == ValueFlow::Value::ValueKind::Known) + it->valueKind = ValueFlow::Value::ValueKind::Possible; + } + } + valueFlowForward(tok, endOfVarScope, var, varid, values, constValue, tokenlist, errorLogger, settings); } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 9943581c0..ece65b843 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1576,6 +1576,14 @@ private: ASSERT_EQUALS(1, value.intvalue); ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind); + code = "void f() {\n" + " static int x = 0;\n" + " return x + 1;\n" // <- possible value + "}\n"; + value = valueOfTok(code, "+"); + ASSERT_EQUALS(1, value.intvalue); + ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind); + code = "void f() {\n" " int x = 0;\n" " do {\n"