diff --git a/lib/token.cpp b/lib/token.cpp index 9e829e35c..31ef9cad2 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2260,9 +2260,18 @@ std::pair Token::typeDecl(const Token * tok) if (Token::Match(tok2, "; %varid% =", var->declarationId())) tok2 = tok2->tokAt(2); if (Token::simpleMatch(tok2, "=") && Token::Match(tok2->astOperand2(), "!!=") && tok != tok2->astOperand2()) { - std::pair r = typeDecl(tok2->astOperand2()); + tok2 = tok2->astOperand2(); + std::pair r = typeDecl(tok2); if (r.first) return r; + if (tok2->astOperand1() && Token::simpleMatch(tok2, "new")) { + if (Token::simpleMatch(tok2->astOperand1(), "(")) + return { tok2->next(), tok2->astOperand1() }; + const Token* declEnd = nextAfterAstRightmostLeaf(tok2->astOperand1()); + if (Token::simpleMatch(declEnd, "<") && declEnd->link()) + declEnd = declEnd->link()->next(); + return { tok2->next(), declEnd }; + } } if (astIsRangeBasedForDecl(var->nameToken()) && astIsContainer(var->nameToken()->astParent()->astOperand2())) { // range-based for const ValueType* vt = var->nameToken()->astParent()->astOperand2()->valueType(); diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 585007362..5ba700e26 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1906,6 +1906,16 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function s.find() is not used.\n", errout.str()); + check("void f() {\n" + " auto* p = new std::vector(5);\n" + " p->push_back(1);\n" + " auto* q = new std::vector{ 5, 7 };\n" + " q->push_back(1);\n" + " auto* r = new std::vector;\n" + " r->push_back(1);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + settings.severity = severity_old; settings.checkLibrary = false; }