diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 031fa0525..e005e0e71 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -399,6 +399,10 @@ void CheckAutoVariables::returnReference() // have we reached a function that returns a reference? if (tok->previous() && tok->previous()->str() == "&") { for (const Token *tok2 = scope->classStart->next(); tok2 && tok2 != scope->classEnd; tok2 = tok2->next()) { + // Skip over lambdas + if (tok2->str() == "[" && tok2->link()->strAt(1) == "(" && tok2->link()->linkAt(1)->strAt(1) == "{") + tok2 = tok2->link()->linkAt(1)->linkAt(1); + if (tok2->str() != "return") continue; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index d90cf14df..a8bf0c94b 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -98,6 +98,7 @@ private: TEST_CASE(returnReference7); TEST_CASE(returnReferenceLiteral); TEST_CASE(returnReferenceCalculation); + TEST_CASE(returnReferenceLambda); // global namespace TEST_CASE(testglobalnamespace); @@ -954,6 +955,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void returnReferenceLambda() { + // #6787 + check("const Item& foo(const Container& items) const {\n" + " return bar(items.begin(), items.end(),\n" + " [](const Item& lhs, const Item& rhs) {\n" + " return false;\n" + " });\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } void testglobalnamespace() { check("class SharedPtrHolder\n"