diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 09acaf715..7be6cf3c7 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -430,7 +430,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de Variables::VariableUsage* var2 = variables.find(varid2); if (var2) { // local variable (alias or read it) - if (var1->_type == Variables::pointer) { + if (var1->_type == Variables::pointer || var1->_type == Variables::pointerArray) { if (dereference) variables.read(varid2); else { @@ -439,8 +439,12 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de var2->_type == Variables::pointer) { bool replace = true; + // pointerArray => don't replace + if (var1->_type == Variables::pointerArray) + replace = false; + // check if variable declared in same scope - if (scope == var1->_scope) + else if (scope == var1->_scope) replace = true; // not in same scope as declaration @@ -839,6 +843,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } else if (var->_type == Variables::pointer || var->_type == Variables::reference) { variables.read(varid); variables.writeAliases(varid); + } else if (var->_type == Variables::pointerArray) { + tok = doAssignment(variables, tok, false, scope); } else variables.writeAll(varid); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d41bd434b..f340d58e8 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1185,6 +1185,25 @@ private: " *(++ptr) = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #3910 + functionVariableUsage("int foo() {\n" + " char buf[5];\n" + " char *data[2];\n" + " data[0] = buf;\n" + " do_something(data);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("int foo() {\n" + " char buf1[5];\n" + " char buf2[5];\n" + " char *data[2];\n" + " data[0] = buf1;\n" + " data[1] = buf2;\n" + " do_something(data);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void localvar17() { // ticket #1720