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)