From ac85b78e2a92c5fe23e597038c18841a1a18b4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 9 Jul 2017 11:19:00 +0200 Subject: [PATCH] Fixed #5619 (false positive: unusedVariable - array accessed by pointer variable only) --- lib/checkunusedvar.cpp | 11 +++++++++-- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 26364e99c..e123f9269 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -594,9 +594,16 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de } else { // not a local variable (or an unsupported local variable) if (var1->_type == Variables::pointer && !dereference) { // check if variable declaration is in this scope - if (var1->_var->scope() == scope) + if (var1->_var->scope() == scope) { + // If variable is used in RHS then "use" variable + for (const Token *rhs = tok; rhs && rhs->str() != ";"; rhs = rhs->next()) { + if (rhs->varId() == varid1) { + variables.use(varid1, tok); + break; + } + } variables.clearAliases(varid1); - else { + } else { // no other assignment in this scope if (var1->_assignments.find(scope) == var1->_assignments.end()) { /** diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index bc2f2995e..817e2cbec 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -116,6 +116,7 @@ private: TEST_CASE(localvaralias11); // ticket #4423 - iterator TEST_CASE(localvaralias12); // ticket #4394 TEST_CASE(localvaralias13); // ticket #4487 + TEST_CASE(localvaralias14); // ticket #5619 TEST_CASE(localvarasm); TEST_CASE(localvarstatic); TEST_CASE(localvarextern); @@ -3008,6 +3009,14 @@ private: TODO_ASSERT_EQUALS("a is assigned value that is never used", "", errout.str()); } + void localvaralias14() { // #5619 + functionVariableUsage("void f() {\n" + " char a[4], *p=a;\n" + " p = dostuff(p);\n" + "}"); + TODO_ASSERT_EQUALS("p is assigned a value that is never used", "", errout.str()); + } + void localvarasm() { functionVariableUsage("void foo(int &b)\n" "{\n"