Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading