From c3506b5186f7b6641022c2361682d029535f8dd7 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 13 Mar 2022 06:26:21 +0100 Subject: [PATCH] Fix #10691 regressions with static variables and multiple assignments (#3895) * Fix regressions with static variables and multiple assignments * Fix test * Fix test cases --- lib/checkunusedvar.cpp | 4 ++-- test/testunusedvar.cpp | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 2c3d5c19b..c6f5e0f4c 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -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(); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d937b9d88..5727dd0a5 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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"