From 0c1ba8e8b48246ebbc7d9479744bcc5eab3bf1e6 Mon Sep 17 00:00:00 2001 From: umanamente Date: Sun, 22 Jan 2017 14:16:40 +0500 Subject: [PATCH] (Fixed #7895) C++11 brace initialization. Added test. (#862) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an optional extended description… --- lib/checkunusedvar.cpp | 28 ++++++++++++++++++++++------ test/testunusedvar.cpp | 12 ++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index a62fbfdab..e4997f738 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -730,13 +730,29 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (i->isArray() && Token::Match(i->nameToken(), "%name% [ %var% ]")) // Array index variable read. variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken()); - if (defValTok && defValTok->str() == "=") { - if (defValTok->next() && defValTok->next()->str() == "{") { - for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next()) - if (tok->varId()) // Variables used to initialize the array read. - variables.read(tok->varId(), i->nameToken()); - } else + if (defValTok && defValTok->next()) { + // simple assignment "var = 123" + if (defValTok->str() == "=" && defValTok->next()->str() != "{") { 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() == "{") { + // "var = {...}" + tokBraceStart = defValTok->next(); + } else if (defValTok->str() == "{") { + // "var{...}" + tokBraceStart = defValTok; + } + if (tokBraceStart) { + for (const Token* tok = tokBraceStart->next(); tok && tok != tokBraceStart->link(); tok = tok->next()) { + if (tok->varId()) { + // Variables used to initialize the array read. + variables.read(tok->varId(), i->nameToken()); + } + } + } + } } } } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index a43f45f9d..471f78b4e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -175,6 +175,7 @@ private: TEST_CASE(lambdaFunction); // #5078 TEST_CASE(namespaces); // #7557 + TEST_CASE(bracesInitCpp11);// #7895 - "int var{123}" initialization } void checkStructMemberUsage(const char code[]) { @@ -4101,6 +4102,17 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void bracesInitCpp11() { + functionVariableUsage( + "int fun() {\n" + " static int fpUnread{0};\n" + " const int var{fpUnread++};\n" + " return var;\n" + "}\n" + ); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUnusedVar)