Skip to content

Commit 59f4575

Browse files
Fix #12607 FP returnReference with std::map (regression) (#6278)
1 parent 5879f28 commit 59f4575

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

lib/valueflow.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3665,10 +3665,14 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
36653665

36663666
const Token *vartok = tok;
36673667
while (vartok) {
3668-
if (vartok->str() == "[" || vartok->originalName() == "->" || vartok->isUnaryOp("*"))
3669-
vartok = vartok->astOperand1();
3670-
else if (vartok->str() == ".")
3668+
if (vartok->str() == "[" || vartok->isUnaryOp("*"))
36713669
vartok = vartok->astOperand1();
3670+
else if (vartok->str() == ".") {
3671+
if (vartok->originalName().empty() || !Token::simpleMatch(vartok->astOperand1(), "."))
3672+
vartok = vartok->astOperand1();
3673+
else
3674+
break;
3675+
}
36723676
else if (vartok->str() == "::")
36733677
vartok = vartok->astOperand2();
36743678
else

test/testautovariables.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class TestAutoVariables : public TestFixture {
115115
TEST_CASE(returnReference24); // #10098
116116
TEST_CASE(returnReference25); // #10983
117117
TEST_CASE(returnReference26);
118+
TEST_CASE(returnReference27);
118119
TEST_CASE(returnReferenceFunction);
119120
TEST_CASE(returnReferenceContainer);
120121
TEST_CASE(returnReferenceLiteral);
@@ -1656,6 +1657,18 @@ class TestAutoVariables : public TestFixture {
16561657
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Reference to local variable returned.\n", errout_str());
16571658
}
16581659

1660+
void returnReference27()
1661+
{
1662+
check("struct S {\n" // #12607
1663+
" const std::string & f(const std::string& a, const std::string& b) {\n"
1664+
" auto status = m.emplace(a, b);\n"
1665+
" return status.first->second;\n"
1666+
" }\n"
1667+
" std::map<std::string, std::string> m;\n"
1668+
"};\n");
1669+
ASSERT_EQUALS("", errout_str());
1670+
}
1671+
16591672
void returnReferenceFunction() {
16601673
check("int& f(int& a) {\n"
16611674
" return a;\n"

0 commit comments

Comments
 (0)