Fixed #7630 (FP: dead store, modifying array, index var modified)
This commit is contained in:
parent
cd3818088f
commit
0767284ca1
|
@ -501,9 +501,24 @@ void CheckOther::checkRedundantAssignment()
|
||||||
} else if (tok->tokType() == Token::eVariable && !Token::Match(tok, "%name% (")) {
|
} else if (tok->tokType() == Token::eVariable && !Token::Match(tok, "%name% (")) {
|
||||||
const Token *eq = nullptr;
|
const Token *eq = nullptr;
|
||||||
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
|
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();
|
tok2 = tok2->link();
|
||||||
else if (Token::Match(tok2, "[)];,]"))
|
} else if (Token::Match(tok2, "[)];,]"))
|
||||||
break;
|
break;
|
||||||
else if (tok2->str() == "=") {
|
else if (tok2->str() == "=") {
|
||||||
eq = tok2;
|
eq = tok2;
|
||||||
|
|
|
@ -4577,6 +4577,21 @@ private:
|
||||||
"}", nullptr, false, false, false);
|
"}", 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());
|
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
|
// Testing different types
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" Foo& bar = foo();\n"
|
" Foo& bar = foo();\n"
|
||||||
|
|
Loading…
Reference in New Issue