Skip to content

Commit

Permalink
Fix #11872 FN unusedVariable with multidimensional array
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 16, 2023
1 parent d064f9c commit b29e197
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
else if (i->isArray() && i->nameToken()->previous()->str() == "&")
type = Variables::referenceArray;
else if (i->isArray())
type = (i->dimensions().size() == 1U) ? Variables::array : Variables::pointerArray;
type = Variables::array;
else if (i->isReference() && !(i->valueType() && i->valueType()->type == ValueType::UNKNOWN_TYPE && Token::simpleMatch(i->typeStartToken(), "auto")))
type = Variables::reference;
else if (i->nameToken()->previous()->str() == "*" && i->nameToken()->strAt(-2) == "*")
Expand Down Expand Up @@ -1069,13 +1069,15 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} else if (Token::Match(tok, "[(,] & %var% [,)]")) {
variables.eraseAll(tok->tokAt(2)->varId());
} else if (Token::Match(tok, "[(,] (") &&
Token::Match(tok->next()->link(), ") %var% [,)]")) {
Token::Match(tok->next()->link(), ") %var% [,)[]")) {
variables.use(tok->next()->link()->next()->varId(), tok); // use = read + write
} else if (Token::Match(tok, "[(,] *| %var% =")) {
tok = tok->next();
if (tok->str() == "*")
tok = tok->next();
variables.use(tok->varId(), tok);
} else if (Token::Match(tok, "[(,] *| *| %var%")) {
const Token* vartok = tok->next();
while (vartok->str() == "*")
vartok = vartok->next();
if (!(vartok->variable() && vartok == vartok->variable()->nameToken()) &&
!(tok->str() == "(" && !Token::Match(tok->previous(), "%name%")))
variables.use(vartok->varId(), vartok);
}

// function
Expand Down
17 changes: 17 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6041,6 +6041,23 @@ class TestUnusedVar : public TestFixture {
" dostuff(*p);\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("int foo() {\n"
" int p[5];\n"
" dostuff(*p);\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("int foo() {\n"
" int p[5][5][5];\n"
" dostuff(**p);\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("void f() {\n" // #11872
" char v[1][2];\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: v\n", errout.str());
}

void localvarstring1() { // ticket #1597
Expand Down

0 comments on commit b29e197

Please sign in to comment.