Skip to content

Commit fe38e25

Browse files
committed
Fixed cppcheck-opensource#7907 (FN: redundant assignment inside switchcase, overwritten by assignment outside of switch)
1 parent 4558fd5 commit fe38e25

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ void CheckOther::checkRedundantAssignment()
494494
// there is redundant assignment. Is there a case between the assignments?
495495
bool hasCase = false;
496496
for (const Token *tok2 = tok; tok2 != nextAssign; tok2 = tok2->next()) {
497+
if (tok2->str() == "break" || tok2->str() == "return")
498+
break;
497499
if (tok2->str() == "case") {
498500
hasCase = true;
499501
break;

‎test/testother.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,6 +2284,27 @@ class TestOther : public TestFixture {
22842284
" bar(y);\n"
22852285
"}");
22862286
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());
2287+
2288+
check("bool f() {\n"
2289+
" bool ret = false;\n"
2290+
" switch (switchCond) {\n"
2291+
" case 1:\n"
2292+
" ret = true;\n"
2293+
" break;\n"
2294+
" case 31:\n"
2295+
" ret = true;\n"
2296+
" break;\n"
2297+
" case 54:\n"
2298+
" ret = true;\n"
2299+
" break;\n"
2300+
" };\n"
2301+
" ret = true;\n"
2302+
" return ret;\n"
2303+
"}");
2304+
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n"
2305+
"[test.cpp:8] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n"
2306+
"[test.cpp:11] -> [test.cpp:14]: (style) Variable 'ret' is reassigned a value before the old one has been used.\n",
2307+
errout.str());
22872308
}
22882309

22892310
void switchRedundantBitwiseOperationTest() {

0 commit comments

Comments
 (0)