Fixed #8580 (False positive: unused function (lambda))

This commit is contained in:
Daniel Marjamäki 2019-04-27 17:17:51 +02:00
parent 6fcef867a1
commit 004d7d5333
2 changed files with 20 additions and 2 deletions

View File

@ -20,6 +20,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "checkunusedfunctions.h" #include "checkunusedfunctions.h"
#include "astutils.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "library.h" #include "library.h"
#include "settings.h" #include "settings.h"
@ -91,8 +92,14 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
} }
// Function usage.. // Function usage..
const Token *lambdaEndToken = nullptr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) { 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 // parsing of library code to find called functions
if (settings->library.isexecutableblock(FileName, tok->str())) { if (settings->library.isexecutableblock(FileName, tok->str())) {
const Token * markupVarToken = tok->tokAt(settings->library.blockstartoffset(FileName)); 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; const Token *funcname = nullptr;
if (tok->scope()->isExecutable() && Token::Match(tok, "%name% (")) { if ((lambdaEndToken || tok->scope()->isExecutable()) && Token::Match(tok, "%name% (")) {
funcname = tok; 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; funcname = tok;
} else if (Token::Match(tok, "[;{}.,()[=+-/|!?:]")) { } else if (Token::Match(tok, "[;{}.,()[=+-/|!?:]")) {
funcname = tok->next(); funcname = tok->next();

View File

@ -337,6 +337,17 @@ private:
"int m_i;\n" "int m_i;\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #8580
check("int foo() { return 12345; }\n"
"int bar(std::function<int()> 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() { void member_function_ternary() {