diff --git a/lib/token.cpp b/lib/token.cpp index 682b6e33e..bb5955283 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -18,6 +18,7 @@ #include "token.h" +#include "astutils.h" #include "errorlogger.h" #include "library.h" #include "settings.h" @@ -1173,20 +1174,32 @@ static const Token* goToRightParenthesis(const Token* start, const Token* end) std::pair Token::findExpressionStartEndTokens() const { const Token * const top = this; + + // find start node in AST tree const Token *start = top; while (start->astOperand1() && (start->astOperand2() || !start->isUnaryPreOp() || Token::simpleMatch(start, "( )") || start->str() == "{")) start = start->astOperand1(); + + // find end node in AST tree const Token *end = top; while (end->astOperand1() && (end->astOperand2() || end->isUnaryPreOp())) { - if (Token::Match(end,"(|[") && - !(Token::Match(end, "( %type%") && !end->astOperand2())) { + // lambda.. + if (end->str() == "[") { + const Token *lambdaEnd = findLambdaEndToken(end); + if (lambdaEnd) { + end = lambdaEnd; + break; + } + } else if (Token::Match(end,"(|[") && + !(Token::Match(end, "( %type%") && !end->astOperand2())) { end = end->link(); break; } end = end->astOperand2() ? end->astOperand2() : end->astOperand1(); } + // skip parentheses start = goToLeftParenthesis(start, end); end = goToRightParenthesis(start, end); if (Token::simpleMatch(end, "{")) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index e842aae72..bdc0e4cec 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -181,6 +181,7 @@ private: TEST_CASE(localvarFuncPtr); // #7194 TEST_CASE(localvarAddr); // #7477 TEST_CASE(localvarDelete); + TEST_CASE(localvarReturnCallLambda); // #8941 TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCpp11Initialization); @@ -4232,6 +4233,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarReturnCallLambda() { + functionVariableUsage("int foo() {\n" + " auto f = []{return 1};\n" + " return f();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvarCppInitialization() { functionVariableUsage("void foo() {\n" " int buf[6];\n"