From 9fa5c4e2011a1bc00ce4d1eed6f36a02e966e23f Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 30 Apr 2022 01:23:58 -0500 Subject: [PATCH] Fix 11011: FP returnDanglingLifetime with non-const reference argument [inconclusive] (#4066) * Fix 11011: FP returnDanglingLifetime with non-const reference argument [inconclusive] * Format --- lib/valueflow.cpp | 2 +- test/testautovariables.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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() {