Fix issue 9853: False positive: returnReference when using a pointer to container (#2765)

This commit is contained in:
Paul Fultz II 2020-09-02 13:01:08 -05:00 committed by GitHub
parent 12d51ae5c4
commit 03cefd5d70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -3063,7 +3063,7 @@ std::vector<LifetimeToken> getLifetimeTokens(const Token* tok, ValueFlow::Value:
}
}
return result;
} else if (Token::Match(tok->tokAt(-2), ". %name% (") && astIsContainer(tok->tokAt(-2)->astOperand1())) {
} else if (Token::Match(tok->tokAt(-2), ". %name% (") && tok->tokAt(-2)->originalName() != "->" && astIsContainer(tok->tokAt(-2)->astOperand1())) {
const Library::Container* library = getLibraryContainer(tok->tokAt(-2)->astOperand1());
Library::Container::Yield y = library->getYield(tok->previous()->str());
if (y == Library::Container::Yield::AT_INDEX || y == Library::Container::Yield::ITEM) {

View File

@ -2143,6 +2143,13 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning object that points to local variable 'a' that will be invalid when returning.\n", errout.str());
check("std::vector<int>* g();\n"
"int& f() {\n"
" auto* p = g();\n"
" return p->front();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector<std::vector<int>> g();\n"
"void f() {\n"
" for(auto& x:g())\n"