diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index cae3f8c3f..fd6f1c5aa 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3930,7 +3930,7 @@ static void valueFlowLifetimeConstructor(Token* tok, else ls.byVal(tok, tokenlist, errorLogger, settings); }); - } else { + } else if (!constructor->nestedIn->varlist.empty()) { LifetimeStore::forEach(args, "Passed to constructor of '" + name + "'.", ValueFlow::Value::LifetimeKind::SubObject, diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 52f12bfab..f97034bc3 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3259,6 +3259,16 @@ private: ASSERT_EQUALS( "[test.cpp:8] -> [test.cpp:7] -> [test.cpp:8]: (error) Returning object that points to local variable 'files' that will be invalid when returning.\n", errout.str()); + + check("struct S {\n" + " explicit S(std::string& s);\n" + "}\n" + "S f() {\n" + " std::string m(\"abc\");\n" + " return S(m);\n" + "}\n", + true); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeAggegrateConstructor() {