Fix #11872 FN unusedVariable with multidimensional array (#5334)

This commit is contained in:
chrchr-github 2023-08-20 11:08:17 +02:00 committed by GitHub
parent d691450443
commit 63811b2993
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View File

@ -704,7 +704,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
else if (i->isArray() && i->nameToken()->previous()->str() == "&") else if (i->isArray() && i->nameToken()->previous()->str() == "&")
type = Variables::referenceArray; type = Variables::referenceArray;
else if (i->isArray()) 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"))) else if (i->isReference() && !(i->valueType() && i->valueType()->type == ValueType::UNKNOWN_TYPE && Token::simpleMatch(i->typeStartToken(), "auto")))
type = Variables::reference; type = Variables::reference;
else if (i->nameToken()->previous()->str() == "*" && i->nameToken()->strAt(-2) == "*") else if (i->nameToken()->previous()->str() == "*" && i->nameToken()->strAt(-2) == "*")
@ -1069,13 +1069,15 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} else if (Token::Match(tok, "[(,] & %var% [,)]")) { } else if (Token::Match(tok, "[(,] & %var% [,)]")) {
variables.eraseAll(tok->tokAt(2)->varId()); variables.eraseAll(tok->tokAt(2)->varId());
} else if (Token::Match(tok, "[(,] (") && } 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 variables.use(tok->next()->link()->next()->varId(), tok); // use = read + write
} else if (Token::Match(tok, "[(,] *| %var% =")) { } else if (Token::Match(tok, "[(,] *| *| %var%")) {
tok = tok->next(); const Token* vartok = tok->next();
if (tok->str() == "*") while (vartok->str() == "*")
tok = tok->next(); vartok = vartok->next();
variables.use(tok->varId(), tok); if (!(vartok->variable() && vartok == vartok->variable()->nameToken()) &&
!(tok->str() == "(" && !Token::Match(tok->previous(), "%name%")))
variables.use(vartok->varId(), vartok);
} }
// function // function

View File

@ -4181,7 +4181,6 @@ void uninitvar_tolower(int character)
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
(void)tolower(c1); (void)tolower(c1);
// cppcheck-suppress unassignedVariable
int c2; int c2;
// cppcheck-suppress constVariablePointer // cppcheck-suppress constVariablePointer
int *pc=&c2; int *pc=&c2;
@ -4203,7 +4202,6 @@ void uninitvar_toupper(int character)
// cppcheck-suppress uninitvar // cppcheck-suppress uninitvar
(void)toupper(c1); (void)toupper(c1);
// cppcheck-suppress unassignedVariable
int c2; int c2;
// cppcheck-suppress constVariablePointer // cppcheck-suppress constVariablePointer
int *pc=&c2; int *pc=&c2;

View File

@ -6041,6 +6041,23 @@ private:
" dostuff(*p);\n" " dostuff(*p);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); 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 void localvarstring1() { // ticket #1597