Fixed #10058 (False positive: redundant assignment, there is break)
This commit is contained in:
parent
8fcef7ad0d
commit
bcf6039558
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue