diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index a7a8f74b9..6a385409a 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1013,6 +1013,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // assignment else if ((Token::Match(tok, "%name% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) || (Token::simpleMatch(tok, "* (") && Token::simpleMatch(tok->next()->link(), ") ="))) { + const Token *eq = tok; + while (eq && !eq->isAssignmentOp()) + eq = eq->astParent(); + + const bool deref = eq && eq->astOperand1() && eq->astOperand1()->valueType() && eq->astOperand1()->valueType()->pointer == 0U; + if (tok->str() == "*") { tok = tok->tokAt(2); if (tok->str() == "(") @@ -1031,7 +1037,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.read(varid, tok); variables.writeAliases(varid, tok); } else if (var->_type == Variables::pointerArray) { - tok = doAssignment(variables, tok, false, scope); + tok = doAssignment(variables, tok, deref, scope); } else variables.writeAll(varid, tok); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 1382a1443..47eb96855 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -119,6 +119,7 @@ private: TEST_CASE(localvararray1); // ticket #2780 TEST_CASE(localvararray2); // ticket #3438 TEST_CASE(localvararray3); // ticket #3980 + TEST_CASE(localvararray4); // ticket #4839 TEST_CASE(localvarstring1); TEST_CASE(localvarstring2); // ticket #2929 TEST_CASE(localvarconst1); @@ -3654,6 +3655,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvararray4() { + functionVariableUsage("void foo() {\n" + " int p[1];\n" + " int *pp[0];\n" + " p[0] = 1;\n" + " *pp[0] = p[0];\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarstring1() { // ticket #1597 functionVariableUsage("void foo() {\n" " std::string s;\n"