Skip to content
Merged
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
20 changes: 13 additions & 7 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,23 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
return tok;
// If any of the submembers are borrowed types then stop
if (std::any_of(it.base() - 1, members.cend() - 1, [&](const Token* tok2) {
if (astIsPointer(tok2) || astIsContainerView(tok2) || astIsIterator(tok2))
const Token* obj = tok2;
if (Token::simpleMatch(obj, "["))
obj = tok2->astOperand1();
const Variable* var = obj->variable();
// Check for arrays first since astIsPointer will return true, but an array is not a borrowed type
if (var && var->isArray())
return false;
if (astIsPointer(obj) || astIsContainerView(obj) || astIsIterator(obj))
return true;
if (!astIsUniqueSmartPointer(tok2)) {
if (astIsSmartPointer(tok2))
if (!astIsUniqueSmartPointer(obj)) {
if (astIsSmartPointer(obj))
return true;
const Token* dotTok = tok2->next();
const Token* dotTok = obj->next();
if (!Token::simpleMatch(dotTok, ".")) {
const Token* endTok = nextAfterAstRightmostLeaf(tok2);
const Token* endTok = nextAfterAstRightmostLeaf(obj);
if (!endTok)
dotTok = tok2->next();
dotTok = obj->next();
else if (Token::simpleMatch(endTok, "."))
dotTok = endTok;
else if (Token::simpleMatch(endTok->next(), "."))
Expand All @@ -672,7 +679,6 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
if (Token::simpleMatch(dotTok, ".") && dotTok->originalName() == "->")
return true;
}
const Variable* var = tok2->variable();
return var && var->isReference();
}))
return nullptr;
Expand Down
7 changes: 7 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3063,6 +3063,13 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:3] -> [test.cpp:4]: (error) Returning pointer to local variable 'ptr' that will be invalid when returning.\n",
errout_str());

// #12600
check("struct S { std::unique_ptr<int> p; };\n"
"int* f(const S* s) {\n"
" return s[0].p.get();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void danglingLifetime() {
check("auto f() {\n"
Expand Down