Fixed false negative #4657

This commit is contained in:
PKEuS 2013-03-15 05:00:51 -07:00
parent c600479c20
commit 1e66e0b931
2 changed files with 13 additions and 1 deletions

View File

@ -2149,7 +2149,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (tok == forHeadEnd)
forHeadEnd = 0;
if (noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope.
if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope.
loopVariable = false;
unsigned int indent = 0;
for (const Token* tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { // Ensure that variable isn't used on right side of =, too

View File

@ -69,6 +69,7 @@ private:
TEST_CASE(varScope14);
TEST_CASE(varScope15); // #4573 if-else-if
TEST_CASE(varScope16);
TEST_CASE(varScope17);
TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
@ -905,6 +906,17 @@ private:
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'a' can be reduced.\n", errout.str());
}
void varScope17() {
varScope("void f() {\n"
" int x;\n"
" if (a) {\n"
" x = stuff(x);\n"
" morestuff(x);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str());
}
void checkOldStylePointerCast(const char code[]) {
// Clear the error buffer..
errout.str("");