From e4ee7cd59c393ea13dbf8ed20ed0ccd3fd76e8e5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 2 Dec 2022 20:28:14 +0100 Subject: [PATCH] Get type from 'auto p = new ...' (#4607) * Fix #11223 checkLibraryFunction treats "auto" as type * Use utility function * Get type from 'auto p = new ...' * Fix merge --- lib/token.cpp | 11 ++++++++++- test/testfunctions.cpp | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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; }