From 87f869a1822edfc5da62199995dd21df7045ba50 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 3 Jun 2026 20:00:28 +0200 Subject: [PATCH 1/4] Fix #14812 FN useStlAlgorithm with std::set --- lib/checkstl.cpp | 11 +++++------ test/teststl.cpp | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index c8fcca48f1a..6d277bc0010 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -3116,19 +3116,18 @@ void CheckStlImpl::useStlAlgorithm() bool useLoopVarInMemCall; const Token *memberAccessTok = singleMemberCallInScope(bodyTok, loopVar->varId(), useLoopVarInMemCall, mSettings); if (memberAccessTok && loopType == LoopType::RANGE) { - const Token *memberCallTok = memberAccessTok->astOperand2(); const int contVarId = memberAccessTok->astOperand1()->varId(); if (contVarId == loopVar->varId()) - continue; - if (memberCallTok->str() == "push_back" || - memberCallTok->str() == "push_front" || - memberCallTok->str() == "emplace_back") { + continue; + using Action = Library::Container::Action; + const auto action = astContainerAction(memberAccessTok->astOperand1(), mSettings.library); + if (contains({Action::PUSH, Action::INSERT}, action)) { std::string algo; if (useLoopVarInMemCall) algo = "std::copy"; else algo = "std::transform"; - useStlAlgorithmError(memberCallTok, algo); + useStlAlgorithmError(memberAccessTok->astOperand2(), algo); } continue; } diff --git a/test/teststl.cpp b/test/teststl.cpp index 51fab0e6021..345b9d94274 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -5661,6 +5661,14 @@ class TestStl : public TestFixture { "}\n", dinit(CheckOptions, $.inconclusive = true)); ASSERT_EQUALS("", errout_str()); + + check("void f(const std::vector& v) {\n" // #14812 + " std::set s;\n" + " for (const std::string& a : v) {\n" + " s.insert(a);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:11]: (style) Consider using std::copy algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str()); } void loopAlgoIncrement() { From 1f152069b810d0a8637cf8956a917557ff5db8fe Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 3 Jun 2026 20:02:09 +0200 Subject: [PATCH 2/4] Format [skip ci] --- lib/checkstl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 6d277bc0010..f499bb03b91 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -3118,7 +3118,7 @@ void CheckStlImpl::useStlAlgorithm() if (memberAccessTok && loopType == LoopType::RANGE) { const int contVarId = memberAccessTok->astOperand1()->varId(); if (contVarId == loopVar->varId()) - continue; + continue; using Action = Library::Container::Action; const auto action = astContainerAction(memberAccessTok->astOperand1(), mSettings.library); if (contains({Action::PUSH, Action::INSERT}, action)) { From ca861f2bc709928159a79bb978c7281557be9f0a Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 3 Jun 2026 20:17:23 +0200 Subject: [PATCH 3/4] Suppress --- test/cfg/boost.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/cfg/boost.cpp b/test/cfg/boost.cpp index 2cc4b288cce..27fae71b499 100644 --- a/test/cfg/boost.cpp +++ b/test/cfg/boost.cpp @@ -157,6 +157,7 @@ void test_BOOST_FOREACH_5() { std::set data; BOOST_FOREACH(const int& i, get_data()) + // cppcheck-suppress useStlAlgorithm data.insert(i); } From b15c0260774d1e5b9a9d8e3ef36f6ec74d6c7782 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 3 Jun 2026 21:17:02 +0200 Subject: [PATCH 4/4] Fix, suppress --- gui/checkstatistics.cpp | 10 +++++----- lib/checkother.cpp | 1 + lib/symboldatabase.cpp | 4 ++-- lib/templatesimplifier.cpp | 1 + tools/dmake/dmake.cpp | 1 + 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gui/checkstatistics.cpp b/gui/checkstatistics.cpp index 612e79da63b..ee36707f49d 100644 --- a/gui/checkstatistics.cpp +++ b/gui/checkstatistics.cpp @@ -109,10 +109,10 @@ unsigned CheckStatistics::getCount(const QString &tool, ShowTypes::ShowType type QStringList CheckStatistics::getTools() const { QSet ret; - for (const QString& tool: mStyle.keys()) ret.insert(tool); - for (const QString& tool: mWarning.keys()) ret.insert(tool); - for (const QString& tool: mPerformance.keys()) ret.insert(tool); - for (const QString& tool: mPortability.keys()) ret.insert(tool); - for (const QString& tool: mError.keys()) ret.insert(tool); + std::copy(mStyle.keys().begin(), mStyle.keys().end(), std::inserter(ret, ret.end())); + std::copy(mWarning.keys().begin(), mWarning.keys().end(), std::inserter(ret, ret.end())); + std::copy(mPerformance.keys().begin(), mPerformance.keys().end(), std::inserter(ret, ret.end())); + std::copy(mPortability.keys().begin(), mPortability.keys().end(), std::inserter(ret, ret.end())); + std::copy(mError.keys().begin(), mError.keys().end(), std::inserter(ret, ret.end())); return ret.values(); } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b5fd0dda361..d56c65145fd 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -4584,6 +4584,7 @@ void CheckOtherImpl::checkUnionZeroInit() std::unordered_map unionsByScopeId; const std::vector unions = parseUnions(*symbolDatabase, mSettings); for (const Union &u : unions) { + // cppcheck-suppress useStlAlgorithm - std::transform is cumbersome unionsByScopeId.emplace(u.scope, u); } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index dda3e9b1e0b..3c149ae9086 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1246,6 +1246,7 @@ void SymbolDatabase::createSymbolDatabaseSetTypePointers() { std::unordered_set typenames; for (const Type &t : typeList) { + // cppcheck-suppress useStlAlgorithm - std::transform is cumbersome typenames.insert(t.name()); } @@ -4599,8 +4600,7 @@ void SymbolDatabase::printXml(std::ostream &out) const } // Variables.. - for (const Variable *var : mVariableList) - variables.insert(var); + std::copy(mVariableList.begin(), mVariableList.end(), std::inserter(variables, variables.end())); outs += " \n"; for (const Variable *var : variables) { if (!var) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 1fe1df10037..d2081d2ce99 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -3916,6 +3916,7 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime) std::unordered_map nameOrdinal; int ordinal = 0; for (const auto& decl : mTemplateDeclarations) { + // cppcheck-suppress useStlAlgorithm - std::transform is cumbersome nameOrdinal.emplace(decl.fullName(), ordinal++); } diff --git a/tools/dmake/dmake.cpp b/tools/dmake/dmake.cpp index a434c710631..eaf0a727646 100644 --- a/tools/dmake/dmake.cpp +++ b/tools/dmake/dmake.cpp @@ -297,6 +297,7 @@ static std::vector prioritizelib(const std::vector& li std::map priorities; std::size_t prio = libfiles.size(); for (const auto &l : libfiles) { + // cppcheck-suppress useStlAlgorithm - std::transform is cumbersome priorities.emplace(l, prio--); } priorities["lib/valueflow.cpp"] = 1000;