diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 68a1230d1..35c1d52da 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -243,7 +243,7 @@ bool CheckAutoVariables::returnTemporary(const Token *tok) const bool retvalue = false; // is there such a function that returns a value? for (scope = symbolDatabase->scopeList.begin(); !retref && scope != symbolDatabase->scopeList.end(); ++scope) { - if (scope->type == Scope::eFunction && scope->function->type != Function::eConstructor && scope->function->type != Function::eCopyConstructor) { + if (scope->type == Scope::eFunction && scope->function && scope->function->type != Function::eConstructor && scope->function->type != Function::eCopyConstructor) { if (scope->className == funcname) { retref = scope->classDef->strAt(-1) == "&"; if (!retref) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 0b143c5a4..9a644b93f 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -557,6 +557,12 @@ private: " return foo();\n" "}"); ASSERT_EQUALS("", errout.str()); + + // Don't crash with function in unknown scope (#4076) + check("X& a::Bar() {}" + "X& foo() {" + " return Bar();" + "}"); } void returnReference3() {