Fixed #2206 (False positive: variable is assigned a value that is never used (array initializers))
This commit is contained in:
parent
7b3e7f6467
commit
d250cb5aa2
|
@ -1258,34 +1258,48 @@ void CheckOther::functionVariableUsage()
|
||||||
|
|
||||||
// standard type declaration of array of with possible initialization
|
// standard type declaration of array of with possible initialization
|
||||||
// int i[10]; int j[2] = { 0, 1 }; static int k[2] = { 2, 3 };
|
// int i[10]; int j[2] = { 0, 1 }; static int k[2] = { 2, 3 };
|
||||||
else if (Token::Match(tok, "[;{}] static| %type% %var% [ %any% ] ;|=") &&
|
else if (Token::Match(tok, "[;{}] static| const| %type% *| %var% [ %any% ] ;|=") &&
|
||||||
nextIsStandardType(tok))
|
nextIsStandardType(tok))
|
||||||
{
|
{
|
||||||
|
bool isStatic = false;
|
||||||
|
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
if (tok->str() == "static")
|
if (tok->str() == "static")
|
||||||
|
{
|
||||||
|
tok = tok->next();
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tok->str() == "const")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
|
|
||||||
variables.addVar(tok->tokAt(1), Variables::array, scope,
|
if (tok->str() != "return" && tok->str() != "throw")
|
||||||
tok->tokAt(5)->str() == "=" || tok->str() == "static");
|
|
||||||
|
|
||||||
// check for reading array size from local variable
|
|
||||||
if (tok->tokAt(3)->varId() != 0)
|
|
||||||
variables.read(tok->tokAt(3)->varId());
|
|
||||||
|
|
||||||
// look at initializers
|
|
||||||
if (Token::Match(tok->tokAt(5), "= {"))
|
|
||||||
{
|
{
|
||||||
tok = tok->tokAt(7);
|
bool isPointer = bool(tok->strAt(1) == "*");
|
||||||
while (tok->str() != "}")
|
const Token * const nametok = tok->tokAt(isPointer ? 2 : 1);
|
||||||
|
|
||||||
|
variables.addVar(nametok, isPointer ? Variables::pointerArray : Variables::array, scope,
|
||||||
|
nametok->tokAt(4)->str() == "=" || isStatic);
|
||||||
|
|
||||||
|
// check for reading array size from local variable
|
||||||
|
if (nametok->tokAt(2)->varId() != 0)
|
||||||
|
variables.read(nametok->tokAt(2)->varId());
|
||||||
|
|
||||||
|
// look at initializers
|
||||||
|
if (Token::Match(nametok->tokAt(4), "= {"))
|
||||||
{
|
{
|
||||||
if (Token::Match(tok, "%var%"))
|
tok = nametok->tokAt(6);
|
||||||
variables.read(tok->varId());
|
while (tok->str() != "}")
|
||||||
tok = tok->next();
|
{
|
||||||
|
if (Token::Match(tok, "%var%"))
|
||||||
|
variables.read(tok->varId());
|
||||||
|
tok = tok->next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
tok = nametok->tokAt(3);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
tok = tok->tokAt(4);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer or reference declaration with possible initialization
|
// pointer or reference declaration with possible initialization
|
||||||
|
|
|
@ -75,6 +75,7 @@ private:
|
||||||
TEST_CASE(localvar26); // ticket #1894
|
TEST_CASE(localvar26); // ticket #1894
|
||||||
TEST_CASE(localvar27); // ticket #2160
|
TEST_CASE(localvar27); // ticket #2160
|
||||||
TEST_CASE(localvar28); // ticket #2205
|
TEST_CASE(localvar28); // ticket #2205
|
||||||
|
TEST_CASE(localvar29); // ticket #2206 (array initialization)
|
||||||
TEST_CASE(localvaralias1);
|
TEST_CASE(localvaralias1);
|
||||||
TEST_CASE(localvaralias2); // ticket #1637
|
TEST_CASE(localvaralias2); // ticket #1637
|
||||||
TEST_CASE(localvaralias3); // ticket #1639
|
TEST_CASE(localvaralias3); // ticket #1639
|
||||||
|
@ -1293,6 +1294,16 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void localvar29() // ticket #2206
|
||||||
|
{
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" float s_ranges[] = { 0, 256 };\n"
|
||||||
|
" float* ranges[] = { s_ranges };\n"
|
||||||
|
" cout << ranges[0][0];\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
void localvaralias1()
|
void localvaralias1()
|
||||||
{
|
{
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
|
|
Loading…
Reference in New Issue