diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index f642239fb..2d44003fb 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -407,6 +407,11 @@ 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()) { + if (!tok2->scope()->isExecutable()) { + tok2 = tok2->scope()->classEnd; + continue; + } + // Skip over lambdas if (tok2->str() == "[" && tok2->link()->strAt(1) == "(" && tok2->link()->linkAt(1)->strAt(1) == "{") tok2 = tok2->link()->linkAt(1)->linkAt(1); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 6353c0e9e..383994d20 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(returnReferenceLiteral); TEST_CASE(returnReferenceCalculation); TEST_CASE(returnReferenceLambda); + TEST_CASE(returnReferenceInnerScope); // global namespace TEST_CASE(testglobalnamespace); @@ -966,6 +967,19 @@ private: ASSERT_EQUALS("", errout.str()); } + void returnReferenceInnerScope() { + // #6951 + check("const Callback& make() {\n" + " struct _Wrapper {\n" + " static ulong call(void* o, const void* f, const void*[]) {\n" + " return 1;\n" + " }\n" + " };\n" + " return _make(_Wrapper::call, pmf);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void testglobalnamespace() { check("class SharedPtrHolder\n" "{\n"