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
10 changes: 7 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3665,10 +3665,14 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,

const Token *vartok = tok;
while (vartok) {
if (vartok->str() == "[" || vartok->originalName() == "->" || vartok->isUnaryOp("*"))
vartok = vartok->astOperand1();
else if (vartok->str() == ".")
if (vartok->str() == "[" || vartok->isUnaryOp("*"))
vartok = vartok->astOperand1();
else if (vartok->str() == ".") {
if (vartok->originalName().empty() || !Token::simpleMatch(vartok->astOperand1(), "."))
vartok = vartok->astOperand1();
else
break;
}
else if (vartok->str() == "::")
vartok = vartok->astOperand2();
else
Expand Down
13 changes: 13 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReference24); // #10098
TEST_CASE(returnReference25); // #10983
TEST_CASE(returnReference26);
TEST_CASE(returnReference27);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
Expand Down Expand Up @@ -1656,6 +1657,18 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Reference to local variable returned.\n", errout_str());
}

void returnReference27()
{
check("struct S {\n" // #12607
" const std::string & f(const std::string& a, const std::string& b) {\n"
" auto status = m.emplace(a, b);\n"
" return status.first->second;\n"
" }\n"
" std::map<std::string, std::string> m;\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}

void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"
Expand Down