From 004d7d5333ce3041b52dfaea20aec6e4a771c4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 27 Apr 2019 17:17:51 +0200 Subject: [PATCH] Fixed #8580 (False positive: unused function (lambda)) --- lib/checkunusedfunctions.cpp | 11 +++++++++-- test/testunusedfunctions.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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() {