Fixed #9555 (False positive (unreadVariable): struct is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2020-02-15 16:43:06 +01:00
parent 569b2a4171
commit 8c0ad6a1b9
2 changed files with 12 additions and 1 deletions

View File

@ -712,7 +712,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
return false;
if (tok1->isSigned() != tok2->isSigned())
return false;
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof" && !(tok1->variable() && tok1 == tok1->variable()->nameToken())) {
if (!tok1->function()) {
if (Token::simpleMatch(tok1->previous(), ".")) {
const Token *lhs = tok1->previous();

View File

@ -162,6 +162,7 @@ private:
TEST_CASE(localvarStruct6);
TEST_CASE(localvarStruct7);
TEST_CASE(localvarStruct8);
TEST_CASE(localvarStruct9);
TEST_CASE(localvarStructArray);
TEST_CASE(localvarOp); // Usage with arithmetic operators
@ -3465,6 +3466,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvarStruct9() {
functionVariableUsage("struct XY { int x; int y; };\n"
"\n"
"void foo() {\n"
" struct XY xy(get());\n"
" return xy.x + xy.y;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvarStructArray() {
// #3633 - detect that struct array is assigned a value
functionVariableUsage("void f() {\n"