diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 57dadab82..e06bada7c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3625,8 +3625,9 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger bool isContainerOfPointers = true; const Token* containerTypeToken = tok->valueType()->containerTypeToken; - if (containerTypeToken && Token::simpleMatch(containerTypeToken->previous(), "<") && containerTypeToken->previous()->link()) { - isContainerOfPointers = Token::simpleMatch(containerTypeToken->previous()->link()->previous(), "*"); + if (containerTypeToken) { + ValueType vt = ValueType::parseDecl(containerTypeToken, settings); + isContainerOfPointers = vt.pointer > 0; } LifetimeStore ls; diff --git a/test/teststl.cpp b/test/teststl.cpp index ca054dfaf..d243f0cd7 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4176,6 +4176,28 @@ private: " delete b;\n" "}\n" ,true); ASSERT_EQUALS("", errout.str()); + + check("struct A {};\n" + "void f() {\n" + " std::vector> v;\n" + " A *a = new A();\n" + " v.push_back(a);\n" + " A *b = v.back();\n" + " v.pop_back();\n" + " delete b;\n" + "}\n" ,true); + ASSERT_EQUALS("", errout.str()); + + check("struct A {};\n" + "void f() {\n" + " std::vector> v;\n" + " std::shared_ptr a = std::make_shared();\n" + " v.push_back(a);\n" + " std::shared_ptr b = v.back();\n" + " v.pop_back();\n" + " delete b;\n" + "}\n" ,true); + ASSERT_EQUALS("", errout.str()); } void invalidContainerLoop() {