Partial fix for #12153 FN returnReference (#5629)

This commit is contained in:
chrchr-github 2023-11-06 20:29:13 +01:00 committed by GitHub
parent 704b862a2d
commit a57fc9ace6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -3470,7 +3470,7 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
errorPath.emplace_back(varDeclEndToken, "Passed to reference.");
return {{tok, true, std::move(errorPath)}};
}
if (Token::simpleMatch(varDeclEndToken, "=")) {
if (Token::Match(varDeclEndToken, "=|{")) {
errorPath.emplace_back(varDeclEndToken, "Assigned to reference.");
const Token *vartok = varDeclEndToken->astOperand2();
const bool temporary = isTemporary(true, vartok, nullptr, true);

View File

@ -121,6 +121,7 @@ private:
TEST_CASE(returnReference23);
TEST_CASE(returnReference24); // #10098
TEST_CASE(returnReference25); // #10983
TEST_CASE(returnReference26);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1649,6 +1650,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void returnReference26()
{
check("const int& f() {\n" // #12153
" int x = 234;\n"
" int& s{ x };\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Reference to local variable returned.\n", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"