diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9edf9dcfd..0f25c1b8c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -494,6 +494,8 @@ void CheckOther::checkRedundantAssignment() // there is redundant assignment. Is there a case between the assignments? bool hasCase = false; for (const Token *tok2 = tok; tok2 != nextAssign; tok2 = tok2->next()) { + if (tok2->str() == "break" || tok2->str() == "return") + break; if (tok2->str() == "case") { hasCase = true; break; diff --git a/test/testother.cpp b/test/testother.cpp index 0cd9b7f7d..032d263f0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2284,6 +2284,27 @@ private: " bar(y);\n" "}"); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:10]: (warning) Variable 'y' is reassigned a value before the old one has been used. 'break;' missing?\n", errout.str()); + + check("bool f() {\n" + " bool ret = false;\n" + " switch (switchCond) {\n" + " case 1:\n" + " ret = true;\n" + " break;\n" + " case 31:\n" + " ret = true;\n" + " break;\n" + " case 54:\n" + " ret = true;\n" + " break;\n" + " };\n" + " ret = true;\n" + " return ret;\n" + "}"); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n" + "[test.cpp:8] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n" + "[test.cpp:11] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n", + errout.str()); } void switchRedundantBitwiseOperationTest() {