Fix issue 9597: False positive: Reference to temporary returned if explicitly casted to base class (#2531)

This commit is contained in:
Paul Fultz II 2020-02-11 04:41:41 -06:00 committed by GitHub
parent 583d5e5958
commit e55ddacd18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -504,6 +504,8 @@ static bool iscast(const Token *tok)
tok2 = tok2->link()->next();
if (tok2->str() == ")") {
if (Token::Match(tok2->previous(), "&|&& )"))
return true;
if (Token::simpleMatch(tok2, ") (") && Token::simpleMatch(tok2->linkAt(1), ") ."))
return true;
return type || tok2->strAt(-1) == "*" || Token::simpleMatch(tok2, ") ~") ||

View File

@ -113,6 +113,7 @@ private:
TEST_CASE(returnReference16); // #9433
TEST_CASE(returnReference17); // #9461
TEST_CASE(returnReference18); // #9482
TEST_CASE(returnReference19); // #9597
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1307,6 +1308,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
// #9597
void returnReference19() {
check("struct C : B {\n"
" const B &f() const { return (const B &)*this; }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"