From e55ddacd185fff08404876d2420b89608f5561a3 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 11 Feb 2020 04:41:41 -0600 Subject: [PATCH] Fix issue 9597: False positive: Reference to temporary returned if explicitly casted to base class (#2531) --- lib/tokenlist.cpp | 2 ++ test/testautovariables.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 9c02b35be..6ff326e36 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -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, ") ~") || diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 88fee3eb8..897afc702 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -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"