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..
//---------------------------------------------------------------------------
void CheckOther::CheckVariableScope()
{
// Walk through all tokens..
@ -526,8 +525,7 @@ void CheckOther::CheckVariableScope()
continue;
// Variable declaration?
if (Token::Match(tok1, "%var% %var% ;") ||
Token::Match(tok1, "%var% %var% ="))
if (Token::Match(tok1, "%type% %var% [;=]"))
{
CheckVariableScope_LookupVar(tok1, tok1->strAt(1));
}
@ -550,7 +548,7 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
int indentlevel = 0;
int parlevel = 0;
bool for_or_while = false;
while (indentlevel >= 0 && tok)
while (tok)
{
if (tok->str() == "{")
{
@ -559,6 +557,8 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
else if (tok->str() == "}")
{
if (indentlevel == 0)
break;
--indentlevel;
if (indentlevel == 0)
{
@ -603,8 +603,11 @@ void CheckOther::CheckVariableScope_LookupVar(const Token *tok1, const char varn
tok = tok->next();
}
// Warning if "used" is true
variableScopeError(tok1, varname);
// Warning if this variable:
// * 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(varScope2);
TEST_CASE(varScope3);
TEST_CASE(varScope4);
TEST_CASE(nullpointer1);
TEST_CASE(nullpointer2);
@ -374,6 +375,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope4()
{
varScope("void foo()\n"
"{\n"
" int i;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}