diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index eca84ecf2..5baed82d3 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2865,13 +2865,9 @@ void CheckClass::checkCopyCtorAndEqOperator() for (const Scope * scope : mSymbolDatabase->classAndStructScopes) { - bool hasNonStaticVars = false; - for (std::list::const_iterator var = scope->varlist.cbegin(); var != scope->varlist.cend(); ++var) { - if (!var->isStatic()) { - hasNonStaticVars = true; - break; - } - } + const bool hasNonStaticVars = std::any_of(scope->varlist.begin(), scope->varlist.end(), [](const Variable& var) { + return !var.isStatic(); + }); if (!hasNonStaticVars) continue; diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 045a9ad42..b5b441aae 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -2860,7 +2860,8 @@ void CheckStl::useStlAlgorithm() if (isEarlyExit(condBodyTok)) { const Token *loopVar2 = Token::findmatch(condBodyTok, "%varid%", condBodyTok->link(), loopVar->varId()); std::string algo; - if (loopVar2) + if (loopVar2 || + (isIteratorLoop && loopVar->variable() && precedes(loopVar->variable()->nameToken(), tok))) // iterator declared outside the loop algo = "std::find_if"; else algo = "std::any_of"; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index cea84e997..01ac0a773 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4409,15 +4409,9 @@ Scope::Scope(const SymbolDatabase *check_, const Token *classDef_, const Scope * bool Scope::hasDefaultConstructor() const { - if (numConstructors) { - std::list::const_iterator func; - - for (func = functionList.cbegin(); func != functionList.cend(); ++func) { - if (func->type == Function::eConstructor && func->argCount() == 0) - return true; - } - } - return false; + return numConstructors > 0 && std::any_of(functionList.begin(), functionList.end(), [](const Function& func) { + return func.type == Function::eConstructor && func.argCount() == 0; + }); } AccessControl Scope::defaultAccess() const @@ -5584,6 +5578,8 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const return var->typeScope()->findFunction(tok, var->valueType()->constness == 1); if (var && var->smartPointerType() && var->smartPointerType()->classScope && tok1->next()->originalName() == "->") return var->smartPointerType()->classScope->findFunction(tok, var->valueType()->constness == 1); + if (var && var->iteratorType() && var->iteratorType()->classScope && tok1->next()->originalName() == "->") + return var->iteratorType()->classScope->findFunction(tok, var->valueType()->constness == 1); } else if (Token::simpleMatch(tok->previous()->astOperand1(), "(")) { const Token *castTok = tok->previous()->astOperand1(); if (castTok->isCast()) { @@ -6280,12 +6276,11 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source const Scope *typeScope = vt1->typeScope; if (!typeScope) return; - for (std::list::const_iterator it = typeScope->varlist.cbegin(); it != typeScope->varlist.cend(); ++it) { - if (it->nameToken()->str() == name) { - var = &*it; - break; - } - } + auto it = std::find_if(typeScope->varlist.begin(), typeScope->varlist.end(), [&name](const Variable& v) { + return v.nameToken()->str() == name; + }); + if (it != typeScope->varlist.end()) + var = &*it; } if (var) setValueType(parent, *var); diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 35d023b45..7d7a5ae7b 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -3809,11 +3809,9 @@ void TemplateSimplifier::simplifyTemplates( } for (std::list::const_iterator it = mInstantiatedTemplates.cbegin(); it != mInstantiatedTemplates.cend(); ++it) { - std::list::iterator decl; - for (decl = mTemplateDeclarations.begin(); decl != mTemplateDeclarations.end(); ++decl) { - if (decl->token() == it->token()) - break; - } + auto decl = std::find_if(mTemplateDeclarations.begin(), mTemplateDeclarations.end(), [&it](const TokenAndName& decl) { + return decl.token() == it->token(); + }); if (decl != mTemplateDeclarations.end()) { if (it->isSpecialization()) { // delete the "template < >" diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 63be8faa4..ffacc665a 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1955,9 +1955,7 @@ private: " for (it = l.begin(); it != l.end(); ++it)\n" " it->g(0);\n" "}\n"); - TODO_ASSERT_EQUALS("", - "[test.cpp:5]: (information) --check-library: There is no matching configuration for function F::g()\n", - errout.str()); + ASSERT_EQUALS("", errout.str()); settings = settings_old; } diff --git a/test/teststl.cpp b/test/teststl.cpp index a488e2c2d..43590966d 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -5241,6 +5241,22 @@ private: " return ret;\n" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (style) Consider using std::any_of, std::all_of, std::none_of algorithm instead of a raw loop.\n", errout.str()); + + check("struct T {\n" + " std::vector v0, v1;\n" + " void g();\n" + "};\n" + "void T::g() {\n" + " for (std::vector::const_iterator it0 = v0.cbegin(); it0 != v0.cend(); ++it0) {\n" + " std::vector::iterator it1;\n" + " for (it1 = v1.begin(); it1 != v1.end(); ++it1)\n" + " if (*it0 == *it1)\n" + " break;\n" + " if (it1 != v1.end())\n" + " v1.erase(it1);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9]: (style) Consider using std::find_if algorithm instead of a raw loop.\n", errout.str()); } void loopAlgoMinMax() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 0a2240a12..e2d99725d 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -438,6 +438,7 @@ private: TEST_CASE(findFunction42); TEST_CASE(findFunction43); // #10087 TEST_CASE(findFunction44); // #11182 + TEST_CASE(findFunction45); TEST_CASE(findFunctionContainer); TEST_CASE(findFunctionExternC); TEST_CASE(findFunctionGlobalScope); // ::foo @@ -6959,6 +6960,19 @@ private: } } + void findFunction45() { + GET_SYMBOL_DB("struct S { void g(int); };\n" + "void f(std::vector& v) {\n" + " std::vector::iterator it = v.begin();\n" + " it->g(1);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "g ( 1"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "g"); + ASSERT_EQUALS(1, functok->function()->tokenDef->linenr()); + } void findFunctionContainer() { {