From a5c6ae1e089ad87c076a371beec582cf79b1a919 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 9 Oct 2019 23:52:11 -0500 Subject: [PATCH] Fix issue 9400: FP returnTempReference - triggered by cast (#2259) --- lib/astutils.cpp | 2 ++ test/testautovariables.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e423bcf5c..126674904 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -228,6 +228,8 @@ bool isTemporary(bool cpp, const Token* tok) return isTemporary(cpp, tok->astOperand1()) || isTemporary(cpp, tok->astOperand2()); if (Token::Match(tok, ",|::")) return isTemporary(cpp, tok->astOperand2()); + if (tok->isCast() || (cpp && isCPPCast(tok))) + return isTemporary(cpp, tok->astOperand2()); if (Token::Match(tok, "?|.|[|++|--|%name%|%assign%")) return false; if (tok->isUnaryOp("*")) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 00b82341d..89393f098 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(returnReference10); TEST_CASE(returnReference11); TEST_CASE(returnReference12); + TEST_CASE(returnReference13); TEST_CASE(returnReferenceFunction); TEST_CASE(returnReferenceContainer); TEST_CASE(returnReferenceLiteral); @@ -1213,6 +1214,22 @@ private: ASSERT_EQUALS("", errout.str()); } + void returnReference13() { + check("std::vector v;\n" + "void* vp = &v;\n" + "int& foo(size_t i) {\n" + " return ((std::vector*)vp)->at(i);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("std::vector v;\n" + "void* vp = &v;\n" + "int& foo(size_t i) {\n" + " return static_cast*>(vp)->at(i);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void returnReferenceFunction() { check("int& f(int& a) {\n" " return a;\n"