diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index b9988c40d..9c601743c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -618,6 +618,9 @@ static bool valueFlowForward(Token * const startToken, if (varusage) { varusagelevel = indentlevel; + if (indentlevel < 0 && tok2->str() == "switch") + return false; + // TODO: don't check noreturn scopes if (number_of_if > 0U || Token::findmatch(tok2, "%varid%", start, varid)) { if (settings->debugwarnings) @@ -683,6 +686,7 @@ static bool valueFlowForward(Token * const startToken, const Scope *scope = tok2->scope(); if (scope && scope->type == Scope::eSwitch) { tok2 = const_cast(scope->classEnd); + --indentlevel; continue; } } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 11e4a7a24..6c38a44b9 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -754,6 +754,18 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 7U, 12)); ASSERT_EQUALS(true, testValueOfX(code, 7U, 34)); + + code = "void f() {\n" // #5981 + " int x;\n" + " switch (ab) {\n" + " case A: x = 12; break;\n" + " case B: x = 34; break;\n" + " }\n" + " switch (ab) {\n" + " case A: v = x; break;\n" // <- x is not 34 + " }\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 8U, 34)); } void valueFlowAfterCondition() {