Fixed ticket #374 (False positive: The scope of variable can be limited)

A few refactorings and fixes were made
This commit is contained in:
Daniel Marjamäki 2009-06-12 16:17:51 +02:00
parent cba0d9e130
commit 35b6b2588e
2 changed files with 19 additions and 6 deletions

View File

@ -454,7 +454,6 @@ void CheckOther::CheckUnsignedDivision()
// Check scope of variables.. // Check scope of variables..
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckOther::CheckVariableScope() void CheckOther::CheckVariableScope()
{ {
// Walk through all tokens.. // Walk through all tokens..
@ -526,8 +525,7 @@ void CheckOther::CheckVariableScope()
continue; continue;
// Variable declaration? // Variable declaration?
if (Token::Match(tok1, "%var% %var% ;") || if (Token::Match(tok1, "%type% %var% [;=]"))
Token::Match(tok1, "%var% %var% ="))
{ {
CheckVariableScope_LookupVar(tok1, tok1->strAt(1)); CheckVariableScope_LookupVar(tok1, tok1->strAt(1));
} }
@ -550,7 +548,7 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
int indentlevel = 0; int indentlevel = 0;
int parlevel = 0; int parlevel = 0;
bool for_or_while = false; bool for_or_while = false;
while (indentlevel >= 0 && tok) while (tok)
{ {
if (tok->str() == "{") if (tok->str() == "{")
{ {
@ -559,6 +557,8 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
else if (tok->str() == "}") else if (tok->str() == "}")
{ {
if (indentlevel == 0)
break;
--indentlevel; --indentlevel;
if (indentlevel == 0) if (indentlevel == 0)
{ {
@ -603,8 +603,11 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
tok = tok->next(); tok = tok->next();
} }
// Warning if "used" is true // Warning if this variable:
variableScopeError(tok1, varname); // * not used in this indentlevel
// * used in lower indentlevel
if (!used && used1)
variableScopeError(tok1, varname);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -54,6 +54,7 @@ private:
TEST_CASE(varScope1); TEST_CASE(varScope1);
TEST_CASE(varScope2); TEST_CASE(varScope2);
TEST_CASE(varScope3); TEST_CASE(varScope3);
TEST_CASE(varScope4);
TEST_CASE(nullpointer1); TEST_CASE(nullpointer1);
TEST_CASE(nullpointer2); TEST_CASE(nullpointer2);
@ -374,6 +375,15 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void varScope4()
{
varScope("void foo()\n"
"{\n"
" int i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}