Skip to content

Commit

Permalink
Fix #11879 FN unreadVariable (regression)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 18, 2023
1 parent 6a263ba commit 71438e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
else if (!usage._var->isMaybeUnused() && !usage._modified && !usage._read && var) {
const Token* vnt = var->nameToken();
bool error = false;
if (vnt->next()->isSplittedVarDeclEq()) {
if (vnt->next()->isSplittedVarDeclEq() || (!var->isReference() && vnt->next()->str() == "=")) {
const Token* nextStmt = vnt->tokAt(2);
if (nextStmt->isExpandedMacro()) {
const Token* parent = nextStmt;
Expand Down
14 changes: 11 additions & 3 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5689,9 +5689,8 @@ class TestUnusedVar : public TestFixture {
"{\n"
" static int i = 0;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n",
"",
errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used.\n",
errout.str());

functionVariableUsage("void foo()\n"
"{\n"
Expand Down Expand Up @@ -6288,6 +6287,15 @@ class TestUnusedVar : public TestFixture {
" const ::std::lock_guard g(m);\n"
"}\n");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("void f(const std::string& str, int i) {\n" // #11879
" const std::string s = str;\n"
" switch (i) {\n"
" default:\n"
" break;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
}

void localVarClass() {
Expand Down

0 comments on commit 71438e3

Please sign in to comment.