Fix 11630 and 11620 lifetime issues (#4913)

This commit is contained in:
Paul Fultz II 2023-03-24 07:29:43 -05:00 committed by GitHub
parent e1a4a18528
commit cd21f55cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -4813,8 +4813,15 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, Erro
// container lifetimes // container lifetimes
else if (astIsContainer(tok)) { else if (astIsContainer(tok)) {
Token * parent = astParentSkipParens(tok); Token * parent = astParentSkipParens(tok);
if (!Token::Match(parent, ". %name% (") && if (!parent)
!Token::simpleMatch(parent, "(")) continue;
if (!Token::Match(parent, ". %name% (") && !Token::Match(parent->previous(), "%name% ("))
continue;
// Skip if its a free function that doesnt yield an iterator to the container
if (Token::Match(parent->previous(), "%name% (") &&
!contains({Library::Container::Yield::START_ITERATOR, Library::Container::Yield::END_ITERATOR},
astFunctionYield(parent->previous(), settings)))
continue; continue;
ValueFlow::Value master; ValueFlow::Value master;

View File

@ -3901,6 +3901,14 @@ private:
" std::map<std::string, int> m;\n" " std::map<std::string, int> m;\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #11628
check("std::vector<int>* g();\n"
"void f() {\n"
" std::unique_ptr<std::vector<int>> p(g());\n"
" if (!p) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void danglingLifetimeBorrowedMembers() void danglingLifetimeBorrowedMembers()

View File

@ -5873,6 +5873,15 @@ private:
ASSERT_EQUALS( ASSERT_EQUALS(
"[test.cpp:4]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead.\n", "[test.cpp:4]: (performance) Ineffective call of function 'substr' because a prefix of the string is assigned to itself. Use resize() or pop_back() instead.\n",
errout.str()); errout.str());
// #11630
check("int main(int argc, const char* argv[]) {\n"
" std::vector<std::string> args(argv + 1, argv + argc);\n"
" args.push_back(\"-h\");\n"
" args.front();\n"
"}\n",
true);
ASSERT_EQUALS("", errout.str());
} }
void invalidContainerLoop() { void invalidContainerLoop() {