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"