Fix issue 9432 and 9433: False positive: Reference to temporary returned (#2302)
This commit is contained in:
parent
c3c9559bee
commit
1ef85f9229
|
@ -237,8 +237,14 @@ bool isTemporary(bool cpp, const Token* tok)
|
|||
return false;
|
||||
if (Token::Match(tok, "&|<<|>>") && isLikelyStream(cpp, tok->astOperand1()))
|
||||
return false;
|
||||
if (Token::Match(tok->previous(), "%name% ("))
|
||||
return tok->previous()->function() && !Function::returnsReference(tok->previous()->function(), true);
|
||||
if (Token::Match(tok->previous(), ">|%name% (")) {
|
||||
const Function * f = nullptr;
|
||||
if (tok->previous()->function())
|
||||
f = tok->previous()->function();
|
||||
else if (tok->previous()->link())
|
||||
f = tok->previous()->link()->previous()->function();
|
||||
return f && !Function::returnsReference(tok->previous()->function(), true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,6 +107,8 @@ private:
|
|||
TEST_CASE(returnReference12);
|
||||
TEST_CASE(returnReference13);
|
||||
TEST_CASE(returnReference14);
|
||||
TEST_CASE(returnReference15); // #9432
|
||||
TEST_CASE(returnReference16); // #9433
|
||||
TEST_CASE(returnReferenceFunction);
|
||||
TEST_CASE(returnReferenceContainer);
|
||||
TEST_CASE(returnReferenceLiteral);
|
||||
|
@ -1244,6 +1246,39 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void returnReference15() {
|
||||
check("template <class T>\n"
|
||||
"const int& f() {\n"
|
||||
" static int s;\n"
|
||||
" return s;\n"
|
||||
"}\n"
|
||||
"template <class T>\n"
|
||||
"const int& f(const T&) {\n"
|
||||
" return f<T>();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("template <class T>\n"
|
||||
"int g();\n"
|
||||
"template <class T>\n"
|
||||
"const int& f(const T&) {\n"
|
||||
" return g<T>();\n"
|
||||
"}\n");
|
||||
TODO_ASSERT_EQUALS("error", "", errout.str());
|
||||
}
|
||||
|
||||
void returnReference16() {
|
||||
check("int& f(std::tuple<int>& x) {\n"
|
||||
" return std::get<0>(x);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("int& f(int x) {\n"
|
||||
" return std::get<0>(std::make_tuple(x));\n"
|
||||
"}\n");
|
||||
TODO_ASSERT_EQUALS("error", "", errout.str());
|
||||
}
|
||||
|
||||
void returnReferenceFunction() {
|
||||
check("int& f(int& a) {\n"
|
||||
" return a;\n"
|
||||
|
|
Loading…
Reference in New Issue