Skip to content

Commit fb388e6

Browse files
Fix #14812 FN useStlAlgorithm with std::set (#8629)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent d45d907 commit fb388e6

8 files changed

Lines changed: 23 additions & 12 deletions

File tree

gui/checkstatistics.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ unsigned CheckStatistics::getCount(const QString &tool, ShowTypes::ShowType type
109109
QStringList CheckStatistics::getTools() const
110110
{
111111
QSet<QString> ret;
112-
for (const QString& tool: mStyle.keys()) ret.insert(tool);
113-
for (const QString& tool: mWarning.keys()) ret.insert(tool);
114-
for (const QString& tool: mPerformance.keys()) ret.insert(tool);
115-
for (const QString& tool: mPortability.keys()) ret.insert(tool);
116-
for (const QString& tool: mError.keys()) ret.insert(tool);
112+
std::copy(mStyle.keyBegin(), mStyle.keyEnd(), std::inserter(ret, ret.end()));
113+
std::copy(mWarning.keyBegin(), mWarning.keyEnd(), std::inserter(ret, ret.end()));
114+
std::copy(mPerformance.keyBegin(), mPerformance.keyEnd(), std::inserter(ret, ret.end()));
115+
std::copy(mPortability.keyBegin(), mPortability.keyEnd(), std::inserter(ret, ret.end()));
116+
std::copy(mError.keyBegin(), mError.keyEnd(), std::inserter(ret, ret.end()));
117117
return ret.values();
118118
}

lib/checkother.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4586,6 +4586,7 @@ void CheckOtherImpl::checkUnionZeroInit()
45864586
std::unordered_map<const Scope *, Union> unionsByScopeId;
45874587
const std::vector<Union> unions = parseUnions(*symbolDatabase, mSettings);
45884588
for (const Union &u : unions) {
4589+
// cppcheck-suppress useStlAlgorithm - std::transform is cumbersome
45894590
unionsByScopeId.emplace(u.scope, u);
45904591
}
45914592

lib/checkstl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,19 +3116,18 @@ void CheckStlImpl::useStlAlgorithm()
31163116
bool useLoopVarInMemCall;
31173117
const Token *memberAccessTok = singleMemberCallInScope(bodyTok, loopVar->varId(), useLoopVarInMemCall, mSettings);
31183118
if (memberAccessTok && loopType == LoopType::RANGE) {
3119-
const Token *memberCallTok = memberAccessTok->astOperand2();
31203119
const int contVarId = memberAccessTok->astOperand1()->varId();
31213120
if (contVarId == loopVar->varId())
31223121
continue;
3123-
if (memberCallTok->str() == "push_back" ||
3124-
memberCallTok->str() == "push_front" ||
3125-
memberCallTok->str() == "emplace_back") {
3122+
using Action = Library::Container::Action;
3123+
const auto action = astContainerAction(memberAccessTok->astOperand1(), mSettings.library);
3124+
if (contains({Action::PUSH, Action::INSERT}, action)) {
31263125
std::string algo;
31273126
if (useLoopVarInMemCall)
31283127
algo = "std::copy";
31293128
else
31303129
algo = "std::transform";
3131-
useStlAlgorithmError(memberCallTok, algo);
3130+
useStlAlgorithmError(memberAccessTok->astOperand2(), algo);
31323131
}
31333132
continue;
31343133
}

lib/symboldatabase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ void SymbolDatabase::createSymbolDatabaseSetTypePointers()
12461246
{
12471247
std::unordered_set<std::string> typenames;
12481248
for (const Type &t : typeList) {
1249+
// cppcheck-suppress useStlAlgorithm - std::transform is cumbersome
12491250
typenames.insert(t.name());
12501251
}
12511252

@@ -4599,8 +4600,7 @@ void SymbolDatabase::printXml(std::ostream &out) const
45994600
}
46004601

46014602
// Variables..
4602-
for (const Variable *var : mVariableList)
4603-
variables.insert(var);
4603+
std::copy(mVariableList.begin(), mVariableList.end(), std::inserter(variables, variables.end()));
46044604
outs += " <variables>\n";
46054605
for (const Variable *var : variables) {
46064606
if (!var)

lib/templatesimplifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,7 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
39163916
std::unordered_map<std::string, int> nameOrdinal;
39173917
int ordinal = 0;
39183918
for (const auto& decl : mTemplateDeclarations) {
3919+
// cppcheck-suppress useStlAlgorithm - std::transform is cumbersome
39193920
nameOrdinal.emplace(decl.fullName(), ordinal++);
39203921
}
39213922

test/cfg/boost.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ void test_BOOST_FOREACH_5()
157157
{
158158
std::set<int> data;
159159
BOOST_FOREACH(const int& i, get_data())
160+
// cppcheck-suppress useStlAlgorithm
160161
data.insert(i);
161162
}
162163

test/teststl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5661,6 +5661,14 @@ class TestStl : public TestFixture {
56615661
"}\n",
56625662
dinit(CheckOptions, $.inconclusive = true));
56635663
ASSERT_EQUALS("", errout_str());
5664+
5665+
check("void f(const std::vector<std::string>& v) {\n" // #14812
5666+
" std::set<std::string> s;\n"
5667+
" for (const std::string& a : v) {\n"
5668+
" s.insert(a);\n"
5669+
" }\n"
5670+
"}\n");
5671+
ASSERT_EQUALS("[test.cpp:4:11]: (style) Consider using std::copy algorithm instead of a raw loop. [useStlAlgorithm]\n", errout_str());
56645672
}
56655673

56665674
void loopAlgoIncrement() {

tools/dmake/dmake.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ static std::vector<std::string> prioritizelib(const std::vector<std::string>& li
297297
std::map<std::string, std::size_t> priorities;
298298
std::size_t prio = libfiles.size();
299299
for (const auto &l : libfiles) {
300+
// cppcheck-suppress useStlAlgorithm - std::transform is cumbersome
300301
priorities.emplace(l, prio--);
301302
}
302303
priorities["lib/valueflow.cpp"] = 1000;

0 commit comments

Comments
 (0)