redundantVarAssignment: avoid FPs when loops are used

This commit is contained in:
Daniel Marjamäki 2018-11-25 10:32:30 +01:00
parent 22f736eae2
commit 0160f80ffe
2 changed files with 18 additions and 0 deletions

View File

@ -471,6 +471,12 @@ const Token *CheckOther::checkRedundantAssignmentRecursive(const Token *assign1,
return nullptr; return nullptr;
} }
if (tok->str() == "}" && (tok->scope()->type == Scope::eFor || tok->scope()->type == Scope::eWhile)) {
// TODO: handle loops better
*read = true;
return nullptr;
}
if (Token::Match(tok, "break|continue|return|throw|goto")) { if (Token::Match(tok, "break|continue|return|throw|goto")) {
// TODO: handle these better // TODO: handle these better
*read = true; *read = true;

View File

@ -162,6 +162,7 @@ private:
TEST_CASE(redundantVarAssignment_7133); TEST_CASE(redundantVarAssignment_7133);
TEST_CASE(redundantVarAssignment_stackoverflow); TEST_CASE(redundantVarAssignment_stackoverflow);
TEST_CASE(redundantVarAssignment_lambda); TEST_CASE(redundantVarAssignment_lambda);
TEST_CASE(redundantVarAssignment_for);
TEST_CASE(redundantMemWrite); TEST_CASE(redundantMemWrite);
TEST_CASE(varFuncNullUB); TEST_CASE(varFuncNullUB);
@ -6050,6 +6051,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void redundantVarAssignment_for() {
check("void f() {\n"
" char buf[10];\n"
" int i;\n"
" for (i = 0; i < 4; i++)\n"
" buf[i] = 131;\n"
" buf[i] = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void redundantMemWrite() { void redundantMemWrite() {
return; // FIXME: temporary hack return; // FIXME: temporary hack