diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 023d2ebde31..0695912c00c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2700,7 +2700,8 @@ static void valueFlowLifetimeClassConstructor(Token* tok, if (isDesignatedInitializerArg) ls.argtok = ls.argtok->astOperand2(); const Variable &var = *it; - if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view) + if (ls.argtok->valueType() && ls.argtok->valueType()->pointer && + var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view) return; // TODO: check in isLifetimeBorrowed()? if (var.isReference() || var.isRValueReference()) { ls.byRef(tok, tokenlist, errorLogger, settings); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 2117fe7ba8d..1091a03e9ff 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3957,6 +3957,15 @@ class TestAutoVariables : public TestFixture { "[test.cpp:6:30] -> [test.cpp:6:30] -> [test.cpp:6:21] -> [test.cpp:5:21] -> [test.cpp:8:12]: (error) Returning object that points to local variable 'a' that will be invalid when returning. [returnDanglingLifetime]\n", errout_str()); + check("struct S { const std::string& r; };\n" // #14209 + "S f() {\n" + " std::string s;\n" + " return S{ s };\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:4:15] -> [test.cpp:3:17] -> [test.cpp:4:13]: (error) Returning object that points to local variable 's' that will be invalid when returning. [returnDanglingLifetime]\n", + errout_str()); + check("struct A { int& x; };\n" // #14247 "A f() {\n" " int x = 0;\n"