Fix issue 9536: False positive: Reference to temporary returned when using operator() (#2582)

* Fix issue 9536: False positive: Reference to temporary returned when using operator()

* Add more test cases
This commit is contained in:
Paul Fultz II 2020-04-01 15:35:41 -05:00 committed by GitHub
parent 6cc58e1086
commit 02ae71917a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -254,6 +254,11 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow
return unknown;
}
}
if (tok->isCast())
return false;
// Currying a function is unknown in cppcheck
if (Token::simpleMatch(tok, "(") && Token::simpleMatch(tok->astOperand1(), "("))
return unknown;
return true;
}

View File

@ -114,6 +114,7 @@ private:
TEST_CASE(returnReference17); // #9461
TEST_CASE(returnReference18); // #9482
TEST_CASE(returnReference19); // #9597
TEST_CASE(returnReference20); // #9536
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1316,6 +1317,34 @@ private:
ASSERT_EQUALS("", errout.str());
}
// #9536
void returnReference20() {
check("struct a {\n"
" int& operator()() const;\n"
"};\n"
"int& b() {\n"
" return a()();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("auto a() {\n"
" return []() -> int& {\n"
" static int b;\n"
" return b;\n"
" };\n"
"}\n"
"const int& c() {\n"
" return a()();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::function<int&()> a();\n"
"int& b() {\n"
" return a()();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"