Fixed #1660 (false positive: Variable is assigned a value that is never used, when using cast)
This commit is contained in:
parent
87a54580a8
commit
be35b72c7e
|
@ -774,9 +774,9 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
|
||||||
start++;
|
start++;
|
||||||
|
|
||||||
if (Token::Match(tok->tokAt(start), "&| %var%") ||
|
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), "( 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), "%any% < const| struct|union| %type% *| > ( &| %var%"))
|
||||||
{
|
{
|
||||||
unsigned int offset = 0;
|
unsigned int offset = 0;
|
||||||
unsigned int varid2;
|
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"))
|
if (Token::Match(tok->tokAt(start + 1 + offset), "struct|union"))
|
||||||
offset++;
|
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;
|
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;
|
addressOf = true;
|
||||||
next = start + 6 + offset;
|
next = start + 5 + offset;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
next = start + 5 + offset;
|
next = start + 4 + offset;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
next = start + 4 + offset;
|
next = start + 3 + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for C++ style cast
|
// 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")
|
if (tok->tokAt(start + 2)->str() == "const")
|
||||||
offset++;
|
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"))
|
if (Token::Match(tok->tokAt(start + 2 + offset), "struct|union"))
|
||||||
offset++;
|
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;
|
addressOf = true;
|
||||||
next = start + 7 + offset;
|
next = start + 6 + offset;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
next = start + 6 + offset;
|
next = start + 5 + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
// no cast
|
// no cast
|
||||||
|
|
|
@ -97,6 +97,7 @@ private:
|
||||||
TEST_CASE(localvarOpAssign); // a |= b;
|
TEST_CASE(localvarOpAssign); // a |= b;
|
||||||
TEST_CASE(localvarFor); // for ( ; var; )
|
TEST_CASE(localvarFor); // for ( ; var; )
|
||||||
TEST_CASE(localvarShift); // 1 >> var
|
TEST_CASE(localvarShift); // 1 >> var
|
||||||
|
TEST_CASE(localvarCast);
|
||||||
}
|
}
|
||||||
|
|
||||||
void structmember1()
|
void structmember1()
|
||||||
|
@ -1713,6 +1714,17 @@ private:
|
||||||
ASSERT_EQUALS(std::string(""), errout.str());
|
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)
|
REGISTER_TEST(TestUnusedVar)
|
||||||
|
|
Loading…
Reference in New Issue