Fixed #7630 (FP: dead store, modifying array, index var modified)

This commit is contained in:
Daniel Marjamäki 2016-07-25 07:35:33 +02:00
parent cd3818088f
commit 0767284ca1
2 changed files with 32 additions and 2 deletions

View File

@ -501,9 +501,24 @@ void CheckOther::checkRedundantAssignment()
} else if (tok->tokType() == Token::eVariable && !Token::Match(tok, "%name% (")) {
const Token *eq = nullptr;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "[([]"))
if (Token::Match(tok2, "[([]")) {
// bail out if there is a variable in rhs - we only track 1 variable
bool bailout = false;
for (const Token *tok3 = tok2->link(); tok3 != tok2; tok3 = tok3->previous()) {
if (tok3->varId()) {
if (!tok3->variable())
bailout = true;
else {
const Variable *var = tok3->variable();
if (!var->isConst() || var->isReference() || var->isPointer())
bailout = true;
}
}
}
if (bailout)
break;
tok2 = tok2->link();
else if (Token::Match(tok2, "[)];,]"))
} else if (Token::Match(tok2, "[)];,]"))
break;
else if (tok2->str() == "=") {
eq = tok2;

View File

@ -4577,6 +4577,21 @@ private:
"}", nullptr, false, false, false);
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[2]' is reassigned a value before the old one has been used.\n", errout.str());
check("void f(int x) {\n"
" int i[10];\n"
" i[x] = 1;\n"
" x=1;\n"
" i[x] = 1;\n"
"}", nullptr, false, false, false);
ASSERT_EQUALS("", errout.str());
check("void f(const int x) {\n"
" int i[10];\n"
" i[x] = 1;\n"
" i[x] = 1;\n"
"}", nullptr, false, false, false);
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) Variable 'i[x]' is reassigned a value before the old one has been used.\n", errout.str());
// Testing different types
check("void f() {\n"
" Foo& bar = foo();\n"