Fixed #1660 (false positive: Variable is assigned a value that is never used, when using cast)

This commit is contained in:
Robert Reif 2010-05-09 07:15:40 +02:00 committed by Daniel Marjamäki
parent 87a54580a8
commit be35b72c7e
2 changed files with 33 additions and 14 deletions

View File

@ -774,9 +774,9 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
start++;
if (Token::Match(tok->tokAt(start), "&| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% * ) &| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% * ) ( &| %var%") ||
Token::Match(tok->tokAt(start), "%any% < const| struct|union| %type% * > ( &| %var%"))
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) &| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) ( &| %var%") ||
Token::Match(tok->tokAt(start), "%any% < const| struct|union| %type% *| > ( &| %var%"))
{
unsigned int offset = 0;
unsigned int varid2;
@ -791,27 +791,31 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
if (Token::Match(tok->tokAt(start + 1 + offset), "struct|union"))
offset++;
if (tok->tokAt(start + 4 + offset)->str() == "&")
if (tok->tokAt(start + 2 + offset)->str() == "*")
offset++;
if (tok->tokAt(start + 3 + offset)->str() == "&")
{
addressOf = true;
next = start + 5 + offset;
next = start + 4 + offset;
}
else if (tok->tokAt(start + 4 + offset)->str() == "(")
else if (tok->tokAt(start + 3 + offset)->str() == "(")
{
if (tok->tokAt(start + 5 + offset)->str() == "&")
if (tok->tokAt(start + 4 + offset)->str() == "&")
{
addressOf = true;
next = start + 6 + offset;
next = start + 5 + offset;
}
else
next = start + 5 + offset;
next = start + 4 + offset;
}
else
next = start + 4 + offset;
next = start + 3 + offset;
}
// check for C++ style cast
else if (tok->tokAt(start)->str().find("cast") != std::string::npos)
else if (tok->tokAt(start)->str().find("cast") != std::string::npos &&
tok->tokAt(start + 1)->str() == "<")
{
if (tok->tokAt(start + 2)->str() == "const")
offset++;
@ -819,13 +823,16 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
if (Token::Match(tok->tokAt(start + 2 + offset), "struct|union"))
offset++;
if (tok->tokAt(start + 6 + offset)->str() == "&")
if (tok->tokAt(start + 3 + offset)->str() == "*")
offset++;
if (tok->tokAt(start + 5 + offset)->str() == "&")
{
addressOf = true;
next = start + 7 + offset;
next = start + 6 + offset;
}
else
next = start + 6 + offset;
next = start + 5 + offset;
}
// no cast

View File

@ -97,6 +97,7 @@ private:
TEST_CASE(localvarOpAssign); // a |= b;
TEST_CASE(localvarFor); // for ( ; var; )
TEST_CASE(localvarShift); // 1 >> var
TEST_CASE(localvarCast);
}
void structmember1()
@ -1713,6 +1714,17 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void localvarCast()
{
functionVariableUsage("int foo()\n"
"{\n"
" int a = 1;\n"
" int b = static_cast<int>(a);\n"
" return b;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
};
REGISTER_TEST(TestUnusedVar)