Fix 10098: false negative: 'Reference to temporary returned.' not detected with class (#3509)

This commit is contained in:
Paul Fultz II 2021-10-15 03:59:40 -05:00 committed by GitHub
parent 7f04658585
commit 876702c4f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -371,6 +371,8 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow
return false;
if (const Function * f = ftok->function()) {
return !Function::returnsReference(f, true);
} else if (ftok->type()) {
return true;
} else if (library) {
std::string returnType = library->returnValueType(ftok);
return !returnType.empty() && returnType.back() != '&';

View File

@ -118,6 +118,7 @@ private:
TEST_CASE(returnReference21); // #9530
TEST_CASE(returnReference22);
TEST_CASE(returnReference23);
TEST_CASE(returnReference24); // #10098
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1462,6 +1463,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void returnReference24()
{
check("struct A {\n"
" A() {}\n"
"};\n"
"const A& a() {\n"
" return A();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Reference to temporary returned.\n", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"