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:
parent
9efedd6be9
commit
e4ee7cd59c
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue