Skip to content

Commit

Permalink
Fix #13373 FP returnReference for reference member (danmar#7066)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Dec 4, 2024
1 parent 152dd9d commit 14f3620
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,8 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
if (vartok->str() == "[" || vartok->isUnaryOp("*"))
vartok = vartok->astOperand1();
else if (vartok->str() == ".") {
if (vartok->originalName().empty() || !Token::simpleMatch(vartok->astOperand1(), "."))
if (!Token::simpleMatch(vartok->astOperand1(), ".") &&
!(vartok->astOperand2() && vartok->astOperand2()->valueType() && vartok->astOperand2()->valueType()->reference != Reference::None))
vartok = vartok->astOperand1();
else
break;
Expand Down
12 changes: 12 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReference25); // #10983
TEST_CASE(returnReference26);
TEST_CASE(returnReference27);
TEST_CASE(returnReference28);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
Expand Down Expand Up @@ -1672,6 +1673,17 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void returnReference28()
{
check("struct S {\n" // #13373
" int& r;\n"
"};\n"
"int& f(S s) {\n"
" return s.r;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

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

0 comments on commit 14f3620

Please sign in to comment.