Skip to content

Commit bcf6039

Browse files
committed
Fixed #10058 (False positive: redundant assignment, there is break)
1 parent 8fcef7a commit bcf6039

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,14 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
24292429
return result1;
24302430
if (mWhat == What::ValueFlow && result1.type == Result::Type::WRITE)
24312431
mValueFlowKnown = false;
2432+
if (mWhat == What::Reassign && result1.type == Result::Type::BREAK) {
2433+
const Token *scopeEndToken = findNextTokenFromBreak(result1.token);
2434+
if (scopeEndToken) {
2435+
const Result &result2 = checkRecursive(expr, scopeEndToken->next(), endToken, exprVarIds, local, inInnerClass, depth);
2436+
if (result2.type == Result::Type::BAILOUT)
2437+
return result2;
2438+
}
2439+
}
24322440
if (Token::simpleMatch(tok->linkAt(1), "} else {")) {
24332441
const Token *elseStart = tok->linkAt(1)->tokAt(2);
24342442
const Result &result2 = checkRecursive(expr, elseStart, elseStart->link(), exprVarIds, local, inInnerClass, depth);

test/testother.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class TestOther : public TestFixture {
184184
TEST_CASE(redundantVarAssignment_pointer);
185185
TEST_CASE(redundantVarAssignment_pointer_parameter);
186186
TEST_CASE(redundantVarAssignment_array);
187+
TEST_CASE(redundantVarAssignment_switch_break);
187188
TEST_CASE(redundantInitialization);
188189
TEST_CASE(redundantMemWrite);
189190

@@ -7419,6 +7420,34 @@ class TestOther : public TestFixture {
74197420
ASSERT_EQUALS("", errout.str());
74207421
}
74217422

7423+
void redundantVarAssignment_switch_break() {
7424+
// #10058
7425+
check("void f(int a, int b) {\n"
7426+
" int ret = 0;\n"
7427+
" switch (a) {\n"
7428+
" case 1:\n"
7429+
" ret = 543;\n"
7430+
" if (b) break;\n"
7431+
" ret = 1;\n"
7432+
" break;\n"
7433+
" }"
7434+
" return ret;\n"
7435+
"}");
7436+
ASSERT_EQUALS("", errout.str());
7437+
7438+
check("void f(int a, int b) {\n"
7439+
" int ret = 0;\n"
7440+
" switch (a) {\n"
7441+
" case 1:\n"
7442+
" ret = 543;\n"
7443+
" if (b) break;\n"
7444+
" ret = 1;\n"
7445+
" break;\n"
7446+
" }"
7447+
"}");
7448+
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());
7449+
}
7450+
74227451
void redundantInitialization() {
74237452
setMultiline();
74247453

0 commit comments

Comments
 (0)