Skip to content

Commit

Permalink
Fix #13059 FP returnByReference with function call (#6736)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 30, 2024
1 parent 1583779 commit ecb436a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3327,8 +3327,14 @@ static const Variable* getSingleReturnVar(const Scope* scope) {
if (!start->astOperand1() || start->str() != "return")
return nullptr;
const Token* tok = start->astOperand1();
if (tok->str() == ".")
if (tok->str() == ".") {
const Token* top = tok->astOperand1();
while (Token::Match(top, "[[.]"))
top = top->astOperand1();
if (!Token::Match(top, "%var%"))
return nullptr;
tok = tok->astOperand2();
}
return tok->variable();
}

Expand Down
17 changes: 17 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9083,6 +9083,23 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("[test.cpp:6]: (performance) Function 'get1()' should return member 'str' by const reference.\n"
"[test.cpp:9]: (performance) Function 'get2()' should return member 'strT' by const reference.\n",
errout_str());

checkReturnByReference("struct S { std::string str; };\n" // #13059
"struct T {\n"
" S temp() const;\n"
" S s[1];\n"
"};\n"
"struct U {\n"
" std::string get1() const {\n"
" return t.temp().str;\n"
" }\n"
" std::string get2() const {\n"
" return t.s[0].str;\n"
" }\n"
" T t;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:10]: (performance) Function 'get2()' should return member 'str' by const reference.\n",
errout_str());
}
};

Expand Down

0 comments on commit ecb436a

Please sign in to comment.