diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 3c590576a..906ec15d1 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -68,7 +68,6 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } // Function usage.. - const Token *scopeEnd = nullptr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { // parsing of library code to find called functions @@ -163,31 +162,21 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } } - if (scopeEnd == nullptr) { - if (!Token::Match(tok, ")|= const| {")) - continue; - scopeEnd = tok; - while (scopeEnd->str() != "{") - scopeEnd = scopeEnd->next(); - scopeEnd = scopeEnd->link(); - } else if (tok == scopeEnd) { - scopeEnd = nullptr; - continue; - } - - const Token *funcname = nullptr; - if (Token::Match(tok->next(), "%var% (")) { + if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% (")) { funcname = tok->next(); } - else if (Token::Match(tok->next(), "%var% <") && Token::simpleMatch(tok->linkAt(2), "> (")) { + else if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% <") && Token::simpleMatch(tok->linkAt(2), "> (")) { funcname = tok->next(); } - else if (Token::Match(tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]")) + else if (Token::Match(tok, "[;{}.,()[=+-/|!?:] &| %var% [(),;:}]")) { funcname = tok->next(); + if (tok->str() == "&") + funcname = funcname->next(); + } else if (Token::Match(tok, "[=(,] &| %var% :: %var%")) { funcname = tok->next(); diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 9b3f2943f..80a04a630 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -49,6 +49,7 @@ private: TEST_CASE(operator1); // #3195 TEST_CASE(returnRef); TEST_CASE(attribute); // #3471 - FP __attribute__(constructor) + TEST_CASE(initializer_list); TEST_CASE(multipleFiles); // same function name in multiple files @@ -284,6 +285,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void initializer_list() { + check("int foo() { return 0; }\n" + "struct A {\n" + " A() : m_i(foo())\n" + " {}\n" + "int m_i;\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + void multipleFiles() { CheckUnusedFunctions c;