diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 6c775e0a0..1d9136560 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2429,6 +2429,14 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const return result1; if (mWhat == What::ValueFlow && result1.type == Result::Type::WRITE) mValueFlowKnown = false; + if (mWhat == What::Reassign && result1.type == Result::Type::BREAK) { + const Token *scopeEndToken = findNextTokenFromBreak(result1.token); + if (scopeEndToken) { + const Result &result2 = checkRecursive(expr, scopeEndToken->next(), endToken, exprVarIds, local, inInnerClass, depth); + if (result2.type == Result::Type::BAILOUT) + return result2; + } + } if (Token::simpleMatch(tok->linkAt(1), "} else {")) { const Token *elseStart = tok->linkAt(1)->tokAt(2); const Result &result2 = checkRecursive(expr, elseStart, elseStart->link(), exprVarIds, local, inInnerClass, depth); diff --git a/test/testother.cpp b/test/testother.cpp index cfe2ddb8b..398e69ed4 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -184,6 +184,7 @@ private: TEST_CASE(redundantVarAssignment_pointer); TEST_CASE(redundantVarAssignment_pointer_parameter); TEST_CASE(redundantVarAssignment_array); + TEST_CASE(redundantVarAssignment_switch_break); TEST_CASE(redundantInitialization); TEST_CASE(redundantMemWrite); @@ -7419,6 +7420,34 @@ private: ASSERT_EQUALS("", errout.str()); } + void redundantVarAssignment_switch_break() { + // #10058 + check("void f(int a, int b) {\n" + " int ret = 0;\n" + " switch (a) {\n" + " case 1:\n" + " ret = 543;\n" + " if (b) break;\n" + " ret = 1;\n" + " break;\n" + " }" + " return ret;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(int a, int b) {\n" + " int ret = 0;\n" + " switch (a) {\n" + " case 1:\n" + " ret = 543;\n" + " if (b) break;\n" + " ret = 1;\n" + " break;\n" + " }" + "}"); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:7]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n", errout.str()); + } + void redundantInitialization() { setMultiline();