Skip to content

Commit 63811b2

Browse files
Fix #11872 FN unusedVariable with multidimensional array (#5334)
1 parent d691450 commit 63811b2

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
704704
else if (i->isArray() && i->nameToken()->previous()->str() == "&")
705705
type = Variables::referenceArray;
706706
else if (i->isArray())
707-
type = (i->dimensions().size() == 1U) ? Variables::array : Variables::pointerArray;
707+
type = Variables::array;
708708
else if (i->isReference() && !(i->valueType() && i->valueType()->type == ValueType::UNKNOWN_TYPE && Token::simpleMatch(i->typeStartToken(), "auto")))
709709
type = Variables::reference;
710710
else if (i->nameToken()->previous()->str() == "*" && i->nameToken()->strAt(-2) == "*")
@@ -1069,13 +1069,15 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10691069
} else if (Token::Match(tok, "[(,] & %var% [,)]")) {
10701070
variables.eraseAll(tok->tokAt(2)->varId());
10711071
} else if (Token::Match(tok, "[(,] (") &&
1072-
Token::Match(tok->next()->link(), ") %var% [,)]")) {
1072+
Token::Match(tok->next()->link(), ") %var% [,)[]")) {
10731073
variables.use(tok->next()->link()->next()->varId(), tok); // use = read + write
1074-
} else if (Token::Match(tok, "[(,] *| %var% =")) {
1075-
tok = tok->next();
1076-
if (tok->str() == "*")
1077-
tok = tok->next();
1078-
variables.use(tok->varId(), tok);
1074+
} else if (Token::Match(tok, "[(,] *| *| %var%")) {
1075+
const Token* vartok = tok->next();
1076+
while (vartok->str() == "*")
1077+
vartok = vartok->next();
1078+
if (!(vartok->variable() && vartok == vartok->variable()->nameToken()) &&
1079+
!(tok->str() == "(" && !Token::Match(tok->previous(), "%name%")))
1080+
variables.use(vartok->varId(), vartok);
10791081
}
10801082

10811083
// function

test/cfg/std.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,7 +4181,6 @@ void uninitvar_tolower(int character)
41814181
// cppcheck-suppress uninitvar
41824182
(void)tolower(c1);
41834183

4184-
// cppcheck-suppress unassignedVariable
41854184
int c2;
41864185
// cppcheck-suppress constVariablePointer
41874186
int *pc=&c2;
@@ -4203,7 +4202,6 @@ void uninitvar_toupper(int character)
42034202
// cppcheck-suppress uninitvar
42044203
(void)toupper(c1);
42054204

4206-
// cppcheck-suppress unassignedVariable
42074205
int c2;
42084206
// cppcheck-suppress constVariablePointer
42094207
int *pc=&c2;

test/testunusedvar.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6041,6 +6041,23 @@ class TestUnusedVar : public TestFixture {
60416041
" dostuff(*p);\n"
60426042
"}");
60436043
ASSERT_EQUALS("", errout.str());
6044+
6045+
functionVariableUsage("int foo() {\n"
6046+
" int p[5];\n"
6047+
" dostuff(*p);\n"
6048+
"}");
6049+
ASSERT_EQUALS("", errout.str());
6050+
6051+
functionVariableUsage("int foo() {\n"
6052+
" int p[5][5][5];\n"
6053+
" dostuff(**p);\n"
6054+
"}");
6055+
ASSERT_EQUALS("", errout.str());
6056+
6057+
functionVariableUsage("void f() {\n" // #11872
6058+
" char v[1][2];\n"
6059+
"}");
6060+
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: v\n", errout.str());
60446061
}
60456062

60466063
void localvarstring1() { // ticket #1597

0 commit comments

Comments
 (0)