diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index e4add4ca8..fda9e6fc3 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -20,6 +20,7 @@ //--------------------------------------------------------------------------- #include "checkunusedfunctions.h" +#include "astutils.h" #include "errorlogger.h" #include "library.h" #include "settings.h" @@ -91,8 +92,14 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } // Function usage.. + const Token *lambdaEndToken = nullptr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { + if (tok == lambdaEndToken) + lambdaEndToken = nullptr; + else if (!lambdaEndToken && tok->str() == "[") + lambdaEndToken = findLambdaEndToken(tok); + // parsing of library code to find called functions if (settings->library.isexecutableblock(FileName, tok->str())) { const Token * markupVarToken = tok->tokAt(settings->library.blockstartoffset(FileName)); @@ -192,9 +199,9 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi const Token *funcname = nullptr; - if (tok->scope()->isExecutable() && Token::Match(tok, "%name% (")) { + if ((lambdaEndToken || tok->scope()->isExecutable()) && Token::Match(tok, "%name% (")) { funcname = tok; - } else if (tok->scope()->isExecutable() && Token::Match(tok, "%name% <") && Token::simpleMatch(tok->linkAt(1), "> (")) { + } else if ((lambdaEndToken || tok->scope()->isExecutable()) && Token::Match(tok, "%name% <") && Token::simpleMatch(tok->linkAt(1), "> (")) { funcname = tok; } else if (Token::Match(tok, "[;{}.,()[=+-/|!?:]")) { funcname = tok->next(); diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index e7fde3202..66c299aed 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -337,6 +337,17 @@ private: "int m_i;\n" "};"); ASSERT_EQUALS("", errout.str()); + + // #8580 + check("int foo() { return 12345; }\n" + "int bar(std::function func) { return func(); }\n" + "\n" + "class A {\n" + "public:\n" + " A() : a(bar([] { return foo(); })) {}\n" + " const int a;\n" + "};"); + ASSERT_EQUALS("", errout.str()); } void member_function_ternary() {