diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 898d91737..11cdd5fa5 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1141,6 +1141,10 @@ static bool valueFlowForward(Token * const startToken, if (scope && scope->type == Scope::eSwitch) { tok2 = const_cast(scope->classEnd); --indentlevel; + for (std::list::iterator it = values.begin(); it != values.end(); ++it) { + if (it->valueKind == ValueFlow::Value::ValueKind::Known) + it->valueKind = ValueFlow::Value::ValueKind::Possible; + } continue; } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 035e490bc..9686438cb 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1527,6 +1527,18 @@ private: testValueOfX(code, 2U, 2); // Don't crash (#6494) } + bool isNotKnownValues(const char code[], const char str[]) { + const std::list values = tokenValues(code, str); + bool possible = false; + for (std::list::const_iterator it = values.begin(); it != values.end(); ++it) { + if (it->valueKind == ValueFlow::Value::Known) + return false; + if (it->valueKind == ValueFlow::Value::Possible) + possible = true; + } + return possible; + } + void knownValue() { const char *code; ValueFlow::Value value; @@ -1561,13 +1573,18 @@ private: " }\n" " if (!x) {}\n" // <- possible value "}"; - std::list values = tokenValues(code, "!"); - bool possible = false; - for (std::list::const_iterator it = values.begin(); it != values.end(); ++it) { - if (it->valueKind == ValueFlow::Value::Possible) - possible = true; - } - ASSERT_EQUALS(true, possible); + ASSERT(isNotKnownValues(code, "!")); + + code = "void f() {\n" + " int x = 0;\n" + " switch (state) {\n" + " case 1:\n" + " x = 1;\n" + " break;\n" + " }\n" + " if (!x) {}\n" // <- possible value + "}"; + ASSERT(isNotKnownValues(code, "!")); code = "void f() {\n" " int x = 0;\n"