Fix #10691 regressions with static variables and multiple assignments (#3895)

* Fix regressions with static variables and multiple assignments

* Fix test

* Fix test cases
This commit is contained in:
chrchr-github 2022-03-13 06:26:21 +01:00 committed by GitHub
parent 757287b13c
commit c3506b5186
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -729,7 +729,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
variables.addVar(&*i, type, true);
break;
} else if (defValTok->str() == ";" || defValTok->str() == "," || defValTok->str() == ")") {
variables.addVar(&*i, type, i->isStatic());
variables.addVar(&*i, type, i->isStatic() && i->scope()->type != Scope::eFunction);
break;
}
}
@ -1203,7 +1203,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
continue;
tok = tok->next();
}
if (tok->astParent() && tok->str() != "(") {
if (tok->astParent() && !tok->astParent()->isAssignmentOp() && tok->str() != "(") {
const Token *parent = tok->astParent();
while (Token::Match(parent, "%oror%|%comp%|!|&&"))
parent = parent->astParent();

View File

@ -2578,7 +2578,10 @@ private:
" int a, b, c;\n"
" a = b = c = f();\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used.\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used.\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used.\n"
"[test.cpp:4]: (style) Variable 'c' is assigned a value that is never used.\n",
errout.str());
functionVariableUsage("int * foo()\n"
"{\n"
@ -2886,9 +2889,7 @@ private:
" a = b[c] = 0;\n"
" return a;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b' is assigned a value that is never used.\n",
"",
errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b[c]' is assigned a value that is never used.\n", errout.str());
}
void localvar24() { // ticket #1803
@ -2927,7 +2928,7 @@ private:
" int param = 1;\n"
" ptr->param = param++;\n"
"}");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'param' is assigned a value that is never used.\n", errout.str());
}
void localvar28() { // ticket #2205
@ -5212,8 +5213,15 @@ private:
functionVariableUsage("void foo()\n"
"{\n"
" static int i;\n"
" static const int ci;\n"
" static std::string s;\n"
" static const std::string cs;\n"
"}");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: i\n"
"[test.cpp:4]: (style) Unused variable: ci\n"
"[test.cpp:5]: (style) Unused variable: s\n"
"[test.cpp:6]: (style) Unused variable: cs\n",
errout.str());
functionVariableUsage("void foo()\n"
"{\n"