diff --git a/lib/token.cpp b/lib/token.cpp index 42c450b38..17502ab52 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2255,6 +2255,7 @@ std::pair Token::typeDecl(const Token* tok, bool poi const Variable *var = tok->variable(); if (!var->typeStartToken() || !var->typeEndToken()) return {}; + std::pair result; if (Token::simpleMatch(var->typeStartToken(), "auto")) { const Token * tok2 = var->declEndToken(); if (Token::Match(tok2, "; %varid% =", var->declarationId())) @@ -2272,6 +2273,17 @@ std::pair Token::typeDecl(const Token* tok, bool poi declEnd = declEnd->link()->next(); return { tok2->next(), declEnd }; } + const Token *typeBeg{}, *typeEnd{}; + if (tok2->str() == "::" && Token::simpleMatch(tok2->astOperand2(), "{")) { // empty initlist + typeBeg = previousBeforeAstLeftmostLeaf(tok2); + typeEnd = tok2->astOperand2(); + } + else if (tok2->str() == "{") { + typeBeg = previousBeforeAstLeftmostLeaf(tok2); + typeEnd = tok2; + } + if (typeBeg) + result = { typeBeg->next(), typeEnd }; // handle smart pointers/iterators first } if (astIsRangeBasedForDecl(var->nameToken()) && astIsContainer(var->nameToken()->astParent()->astOperand2())) { // range-based for const ValueType* vt = var->nameToken()->astParent()->astOperand2()->valueType(); @@ -2288,6 +2300,8 @@ std::pair Token::typeDecl(const Token* tok, bool poi if (vt && vt->containerTypeToken) return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) }; } + if (result.first) + return result; } return {var->typeStartToken(), var->typeEndToken()->next()}; } else if (Token::simpleMatch(tok, "return")) { diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 3eb35510d..f7f4e3fab 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1928,6 +1928,29 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f() {\n" + " auto v = std::vector{};\n" + " v.push_back(1);\n" + " auto w = std::vector{ 1, 2, 3 };\n" + " w.push_back(1);\n" + " auto x = std::vector(1);\n" + " x.push_back(1);\n" + "}\n"); + TODO_ASSERT_EQUALS("", + "[test.cpp:7]: (information) --check-library: There is no matching configuration for function auto::push_back()\n", + errout.str()); + + check("void f() {\n" + " auto p(std::make_shared>());\n" + " p->push_back(1);\n" + " auto q{ std::make_shared>{} };\n" + " q->push_back(1);\n" + "}\n"); + TODO_ASSERT_EQUALS("", + "[test.cpp:3]: (information) --check-library: There is no matching configuration for function auto::push_back()\n" + "[test.cpp:5]: (information) --check-library: There is no matching configuration for function auto::push_back()\n", + errout.str()); + settings.severity = severity_old; settings.checkLibrary = false; }