From 0a03bbb32063c13077c63c6d3a8f7283aeb1e893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Jul 2017 23:32:14 +0200 Subject: [PATCH] Fixed #6315 (false positive - unreadVariable - variable is used via pointer) --- lib/checkunusedvar.cpp | 10 ++++++++-- test/testunusedvar.cpp | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e123f9269..6c8cf11c4 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -770,8 +770,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const doAssignment(variables, i->nameToken(), false, scope); } else { // could be "var = {...}" OR "var{...}" (since C++11) - const Token* tokBraceStart = NULL; - if (defValTok->str() == "=" && defValTok->next()->str() == "{") { + const Token* tokBraceStart = nullptr; + if (Token::simpleMatch(defValTok, "= {")) { // "var = {...}" tokBraceStart = defValTok->next(); } else if (defValTok->str() == "{") { @@ -902,6 +902,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const tok = tok->next(); if (!var->isReference()) variables.read(tok->varId(), tok); + } else if (tok->str() == "[" && Token::simpleMatch(skipBrackets(tok),"= {")) { + const Token * const rhs1 = skipBrackets(tok)->next(); + for (const Token *rhs = rhs1->link(); rhs != rhs1; rhs = rhs->previous()) { + if (rhs->varId()) + variables.readAll(rhs->varId(), rhs); + } } else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector tok = tok->previous(); break; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 817e2cbec..55640e010 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(localvaralias12); // ticket #4394 TEST_CASE(localvaralias13); // ticket #4487 TEST_CASE(localvaralias14); // ticket #5619 + TEST_CASE(localvaralias15); // ticket #6315 TEST_CASE(localvarasm); TEST_CASE(localvarstatic); TEST_CASE(localvarextern); @@ -3017,6 +3018,16 @@ private: TODO_ASSERT_EQUALS("p is assigned a value that is never used", "", errout.str()); } + void localvaralias15() { // #6315 + functionVariableUsage("void f() {\n" + " int x=3;\n" + " int *p = &x;\n" + " int *p2[1] = {p};\n" + " dostuff(p2);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarasm() { functionVariableUsage("void foo(int &b)\n" "{\n"