false positive: Variable is assigned a value that is never used

This commit is contained in:
Robert Reif 2010-05-23 10:41:05 +02:00 committed by Daniel Marjamäki
parent 8f76aea6ed
commit 048733a15e
2 changed files with 86 additions and 2 deletions

View File

@ -985,6 +985,11 @@ void CheckOther::functionVariableUsage()
{
variables.addVar(tok->tokAt(2), Variables::array,
tok->tokAt(6)->str() == "=");
// check for reading array size from local variable
if (tok->tokAt(4)->varId() != 0)
variables.read(tok->tokAt(4)->varId());
tok = tok->tokAt(5);
}
@ -1182,6 +1187,11 @@ void CheckOther::functionVariableUsage()
variables.addVar(tok->tokAt(3),
tok->tokAt(2)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
tok->tokAt(7)->str() == "=");
// check for reading array size from local variable
if (tok->tokAt(5)->varId() != 0)
variables.read(tok->tokAt(5)->varId());
tok = tok->tokAt(6);
}
}
@ -1193,6 +1203,11 @@ void CheckOther::functionVariableUsage()
variables.addVar(tok->tokAt(4),
tok->tokAt(3)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
tok->tokAt(8)->str() == "=");
// check for reading array size from local variable
if (tok->tokAt(6)->varId() != 0)
variables.read(tok->tokAt(6)->varId());
tok = tok->tokAt(7);
}
@ -1203,7 +1218,12 @@ void CheckOther::functionVariableUsage()
variables.addVar(tok->tokAt(4),
tok->tokAt(3)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
tok->tokAt(8)->str() == "=");
tok = tok->tokAt(6);
// check for reading array size from local variable
if (tok->tokAt(6)->varId() != 0)
variables.read(tok->tokAt(6)->varId());
tok = tok->tokAt(7);
}
// const array of pointer or reference of struct or union declaration with possible initialization
@ -1213,7 +1233,12 @@ void CheckOther::functionVariableUsage()
variables.addVar(tok->tokAt(5),
tok->tokAt(4)->str() == "*" ? Variables::pointerArray : Variables::referenceArray,
tok->tokAt(9)->str() == "=");
tok = tok->tokAt(7);
// check for reading array size from local variable
if (tok->tokAt(7)->varId() != 0)
variables.read(tok->tokAt(7)->varId());
tok = tok->tokAt(8);
}
else if (Token::Match(tok, "delete|return %var%"))

View File

@ -77,6 +77,7 @@ private:
TEST_CASE(localvar12);
TEST_CASE(localvar13); // ticket #1640
TEST_CASE(localvar14); // ticket #5
TEST_CASE(localvar15);
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
@ -1061,6 +1062,64 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n", errout.str());
}
void localvar15()
{
{
functionVariableUsage("int foo()\n"
"{\n"
" int a = 5;\n"
" int b[a];\n"
" b[0] = 0;\n"
" return b[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
functionVariableUsage("int foo()\n"
"{\n"
" int a = 5;\n"
" int * b[a];\n"
" b[0] = &c;\n"
" return *b[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
functionVariableUsage("int * foo()\n"
"{\n"
" int a = 5;\n"
" const int * b[a];\n"
" b[0] = &c;\n"
" return b[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
functionVariableUsage("struct B * foo()\n"
"{\n"
" int a = 5;\n"
" struct B * b[a];\n"
" b[0] = &c;\n"
" return b[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
functionVariableUsage("const struct B * foo()\n"
"{\n"
" int a = 5;\n"
" const struct B * b[a];\n"
" b[0] = &c;\n"
" return b[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}
void localvaralias1()
{
functionVariableUsage("void foo()\n"