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
This commit is contained in:
chrchr-github 2022-12-02 20:28:14 +01:00 committed by GitHub
parent 9efedd6be9
commit e4ee7cd59c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -2260,9 +2260,18 @@ std::pair<const Token*, const Token*> 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<const Token*, const Token*> r = typeDecl(tok2->astOperand2());
tok2 = tok2->astOperand2();
std::pair<const Token*, const Token*> 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();

View File

@ -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<int>(5);\n"
" p->push_back(1);\n"
" auto* q = new std::vector<int>{ 5, 7 };\n"
" q->push_back(1);\n"
" auto* r = new std::vector<int>;\n"
" r->push_back(1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
settings.severity = severity_old;
settings.checkLibrary = false;
}