Fixed #4155 (false positive: Variable is assigned a value that is never used (inside BOOST_FOREACH loop))

This commit is contained in:
Daniel Marjamäki 2012-12-17 17:07:56 +01:00
parent a84d21f271
commit 72ea94bf75
2 changed files with 21 additions and 0 deletions

View File

@ -746,6 +746,14 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
break; break;
} }
if (Token::Match(tok,"%var% (") && Token::simpleMatch(tok->linkAt(1),") {") && !Token::Match(tok, "for|while|if|catch")) {
const Token * const endTok = tok->linkAt(1)->linkAt(1);
for (const Token *tok2 = endTok->link(); tok2 && tok2 != endTok; tok2 = tok2->next()) {
if (tok2->varId())
variables.erase(tok2->varId());
}
}
if (Token::Match(tok->previous(), "[;{}]")) { if (Token::Match(tok->previous(), "[;{}]")) {
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->varId()) { if (tok2->varId()) {

View File

@ -135,6 +135,7 @@ private:
TEST_CASE(localvarIfElse); // return tmp1 ? tmp2 : tmp3; TEST_CASE(localvarIfElse); // return tmp1 ? tmp2 : tmp3;
TEST_CASE(localvarOpAssign); // a |= b; TEST_CASE(localvarOpAssign); // a |= b;
TEST_CASE(localvarFor); // for ( ; var; ) TEST_CASE(localvarFor); // for ( ; var; )
TEST_CASE(localvarForEach); // BOOST_FOREACH, hlist_for_each, etc
TEST_CASE(localvarShift1); // 1 >> var TEST_CASE(localvarShift1); // 1 >> var
TEST_CASE(localvarShift2); // x = x >> 1 TEST_CASE(localvarShift2); // x = x >> 1
TEST_CASE(localvarShift3); // x << y TEST_CASE(localvarShift3); // x << y
@ -2948,6 +2949,18 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvarForEach() {
functionVariableUsage("void foo() {\n"
" int i = -1;\n"
" int a[] = {1,2,3};\n"
" FOREACH_X (int x, a) {\n"
" if (i==x) return x;\n"
" i = x;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarShift1() { void localvarShift1() {
functionVariableUsage("int foo()\n" functionVariableUsage("int foo()\n"
"{\n" "{\n"