Skip to content

Commit 7189b30

Browse files
authored
fixed some modernize-loop-convert clang-tidy warnings (danmar#2815)
1 parent 98b6238 commit 7189b30

16 files changed

Lines changed: 138 additions & 157 deletions

gui/erroritem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ QString ErrorItem::toString() const
8080
str += GuiSeverity::toString(severity) +"\n";
8181
str += summary + "\n";
8282
str += message + "\n";
83-
for (int i = 0; i < errorPath.size(); i++) {
84-
str += " " + errorPath[i].file + ": " + QString::number(errorPath[i].line) + "\n";
83+
for (const QErrorPathItem& i : errorPath) {
84+
str += " " + i.file + ": " + QString::number(i.line) + "\n";
8585
}
8686
return str;
8787
}

gui/mainwindow.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,9 +1042,9 @@ void MainWindow::analysisDone()
10421042

10431043
enableResultsButtons();
10441044

1045-
for (int i = 0; i < MaxRecentProjects + 1; i++) {
1046-
if (mRecentProjectActs[i] != nullptr)
1047-
mRecentProjectActs[i]->setEnabled(true);
1045+
for (QAction* recentProjectAct : mRecentProjectActs) {
1046+
if (recentProjectAct != nullptr)
1047+
recentProjectAct->setEnabled(true);
10481048
}
10491049

10501050
// Notify user - if the window is not active - that check is ready
@@ -1068,9 +1068,9 @@ void MainWindow::checkLockDownUI()
10681068
if (mScratchPad)
10691069
mScratchPad->setEnabled(false);
10701070

1071-
for (int i = 0; i < MaxRecentProjects + 1; i++) {
1072-
if (mRecentProjectActs[i] != nullptr)
1073-
mRecentProjectActs[i]->setEnabled(false);
1071+
for (QAction* recentProjectAct : mRecentProjectActs) {
1072+
if (recentProjectAct != nullptr)
1073+
recentProjectAct->setEnabled(false);
10741074
}
10751075
}
10761076

@@ -1772,9 +1772,9 @@ void MainWindow::openRecentProject()
17721772

17731773
void MainWindow::updateMRUMenuItems()
17741774
{
1775-
for (int i = 0; i < MaxRecentProjects + 1; i++) {
1776-
if (mRecentProjectActs[i] != nullptr)
1777-
mUI.mMenuFile->removeAction(mRecentProjectActs[i]);
1775+
for (QAction* recentProjectAct : mRecentProjectActs) {
1776+
if (recentProjectAct != nullptr)
1777+
mUI.mMenuFile->removeAction(recentProjectAct);
17781778
}
17791779

17801780
QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();

gui/projectfiledialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
160160

161161
// Platforms..
162162
Platforms platforms;
163-
for (int i = 0; i < numberOfBuiltinPlatforms; i++)
164-
mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatforms[i]).mTitle);
163+
for (cppcheck::Platform::PlatformType builtinPlatform : builtinPlatforms)
164+
mUI.mComboBoxPlatform->addItem(platforms.get(builtinPlatform).mTitle);
165165
QStringList platformFiles;
166166
foreach (QString sp, searchPaths) {
167167
if (sp.endsWith("/cfg"))

gui/resultstree.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
726726
mContextItem = mModel.itemFromIndex(index);
727727
if (mContextItem && mApplications->getApplicationCount() > 0 && mContextItem->parent()) {
728728
//Disconnect all signals
729-
for (int i = 0; i < actions.size(); i++) {
730-
731-
disconnect(actions[i], SIGNAL(triggered()), signalMapper, SLOT(map()));
729+
for (QAction* action : actions) {
730+
disconnect(action, SIGNAL(triggered()), signalMapper, SLOT(map()));
732731
}
733732

734733
disconnect(signalMapper, SIGNAL(mapped(int)),

gui/threadhandler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ void ThreadHandler::setThreadCount(const int count)
136136

137137
void ThreadHandler::removeThreads()
138138
{
139-
for (int i = 0; i < mThreads.size(); i++) {
140-
mThreads[i]->terminate();
141-
disconnect(mThreads[i], &CheckThread::done,
139+
for (CheckThread* thread : mThreads) {
140+
thread->terminate();
141+
disconnect(thread, &CheckThread::done,
142142
this, &ThreadHandler::threadDone);
143-
disconnect(mThreads[i], &CheckThread::fileChecked,
143+
disconnect(thread, &CheckThread::fileChecked,
144144
&mResults, &ThreadResult::fileChecked);
145-
delete mThreads[i];
145+
delete thread;
146146
}
147147

148148
mThreads.clear();
@@ -175,8 +175,8 @@ void ThreadHandler::stop()
175175
{
176176
mCheckStartTime = QDateTime();
177177
mAnalyseWholeProgram = false;
178-
for (int i = 0; i < mThreads.size(); i++) {
179-
mThreads[i]->stop();
178+
for (CheckThread* thread : mThreads) {
179+
thread->stop();
180180
}
181181
}
182182

lib/checkinternal.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ namespace {
3737
void CheckInternal::checkTokenMatchPatterns()
3838
{
3939
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
40-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
41-
const Scope * scope = symbolDatabase->functionScopes[i];
40+
for (const Scope *scope : symbolDatabase->functionScopes) {
4241
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
4342
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
4443
continue;
@@ -130,8 +129,7 @@ void CheckInternal::checkRedundantTokCheckError(const Token* tok)
130129
void CheckInternal::checkTokenSimpleMatchPatterns()
131130
{
132131
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
133-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
134-
const Scope * scope = symbolDatabase->functionScopes[i];
132+
for (const Scope* scope : symbolDatabase->functionScopes) {
135133
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
136134
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
137135
continue;
@@ -151,9 +149,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
151149

152150
// Check for [xyz] usage - but exclude standalone square brackets
153151
unsigned int char_count = 0;
154-
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
155-
char c = pattern[pos];
156-
152+
for (char c : pattern) {
157153
if (c == ' ') {
158154
char_count = 0;
159155
} else if (c == ']') {
@@ -168,9 +164,7 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
168164

169165
// Check | usage: Count characters before the symbol
170166
char_count = 0;
171-
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
172-
const char c = pattern[pos];
173-
167+
for (char c : pattern) {
174168
if (c == ' ') {
175169
char_count = 0;
176170
} else if (c == '|') {
@@ -219,8 +213,7 @@ namespace {
219213
void CheckInternal::checkMissingPercentCharacter()
220214
{
221215
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
222-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
223-
const Scope * scope = symbolDatabase->functionScopes[i];
216+
for (const Scope* scope : symbolDatabase->functionScopes) {
224217
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
225218
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
226219
continue;
@@ -262,8 +255,7 @@ void CheckInternal::checkMissingPercentCharacter()
262255
void CheckInternal::checkUnknownPattern()
263256
{
264257
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
265-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
266-
const Scope * scope = symbolDatabase->functionScopes[i];
258+
for (const Scope* scope : symbolDatabase->functionScopes) {
267259
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
268260
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
269261
continue;
@@ -297,8 +289,7 @@ void CheckInternal::checkUnknownPattern()
297289
void CheckInternal::checkRedundantNextPrevious()
298290
{
299291
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
300-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
301-
const Scope * scope = symbolDatabase->functionScopes[i];
292+
for (const Scope* scope : symbolDatabase->functionScopes) {
302293
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
303294
if (tok->str() != ".")
304295
continue;
@@ -329,8 +320,7 @@ void CheckInternal::checkRedundantNextPrevious()
329320
void CheckInternal::checkExtraWhitespace()
330321
{
331322
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
332-
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
333-
const Scope * scope = symbolDatabase->functionScopes[i];
323+
for (const Scope* scope : symbolDatabase->functionScopes) {
334324
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
335325
if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch ("))
336326
continue;

lib/checkio.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,22 @@ void CheckIO::checkFileUsage()
146146
indent++;
147147
else if (tok->str() == "}") {
148148
indent--;
149-
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
150-
if (indent < i->second.mode_indent) {
151-
i->second.mode_indent = 0;
152-
i->second.mode = UNKNOWN_OM;
149+
for (std::pair<const int, Filepointer>& filepointer : filepointers) {
150+
if (indent < filepointer.second.mode_indent) {
151+
filepointer.second.mode_indent = 0;
152+
filepointer.second.mode = UNKNOWN_OM;
153153
}
154-
if (indent < i->second.op_indent) {
155-
i->second.op_indent = 0;
156-
i->second.lastOperation = Filepointer::UNKNOWN_OP;
154+
if (indent < filepointer.second.op_indent) {
155+
filepointer.second.op_indent = 0;
156+
filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
157157
}
158158
}
159159
} else if (tok->str() == "return" || tok->str() == "continue" || tok->str() == "break" || mSettings->library.isnoreturn(tok)) { // Reset upon return, continue or break
160-
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
161-
i->second.mode_indent = 0;
162-
i->second.mode = UNKNOWN_OM;
163-
i->second.op_indent = 0;
164-
i->second.lastOperation = Filepointer::UNKNOWN_OP;
160+
for (std::pair<const int, Filepointer>& filepointer : filepointers) {
161+
filepointer.second.mode_indent = 0;
162+
filepointer.second.mode = UNKNOWN_OM;
163+
filepointer.second.op_indent = 0;
164+
filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
165165
}
166166
} else if (Token::Match(tok, "%var% =") &&
167167
(tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile" &&
@@ -236,13 +236,13 @@ void CheckIO::checkFileUsage()
236236
const Token* const end2 = tok->linkAt(1);
237237
if (scope->functionOf && scope->functionOf->isClassOrStruct() && !scope->function->isStatic() && ((tok->strAt(-1) != "::" && tok->strAt(-1) != ".") || tok->strAt(-2) == "this")) {
238238
if (!tok->function() || (tok->function()->nestedIn && tok->function()->nestedIn->isClassOrStruct())) {
239-
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
240-
const Variable* var = symbolDatabase->getVariableFromVarId(i->first);
239+
for (std::pair<const int, Filepointer>& filepointer : filepointers) {
240+
const Variable* var = symbolDatabase->getVariableFromVarId(filepointer.first);
241241
if (!var || !(var->isLocal() || var->isGlobal() || var->isStatic())) {
242-
i->second.mode = UNKNOWN_OM;
243-
i->second.mode_indent = 0;
244-
i->second.op_indent = indent;
245-
i->second.lastOperation = Filepointer::UNKNOWN_OP;
242+
filepointer.second.mode = UNKNOWN_OM;
243+
filepointer.second.mode_indent = 0;
244+
filepointer.second.op_indent = indent;
245+
filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
246246
}
247247
}
248248
continue;
@@ -326,10 +326,10 @@ void CheckIO::checkFileUsage()
326326
}
327327
}
328328
}
329-
for (std::map<int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
330-
i->second.op_indent = 0;
331-
i->second.mode = UNKNOWN_OM;
332-
i->second.lastOperation = Filepointer::UNKNOWN_OP;
329+
for (std::pair<const int, Filepointer>& filepointer : filepointers) {
330+
filepointer.second.op_indent = 0;
331+
filepointer.second.mode = UNKNOWN_OM;
332+
filepointer.second.lastOperation = Filepointer::UNKNOWN_OP;
333333
}
334334
}
335335
}

lib/checkother.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,12 +1979,11 @@ namespace {
19791979
for (const Function &func : scope.functionList) {
19801980
functionsByName[func.tokenDef->str()].push_back(&func);
19811981
}
1982-
for (StringFunctionMap::iterator it = functionsByName.begin();
1983-
it != functionsByName.end(); ++it) {
1984-
const std::list<const Function*>::const_iterator nc = std::find_if(it->second.begin(), it->second.end(), notconst);
1985-
if (nc == it->second.end()) {
1982+
for (std::pair<const std::string, std::list<const Function*>>& it : functionsByName) {
1983+
const std::list<const Function*>::const_iterator nc = std::find_if(it.second.begin(), it.second.end(), notconst);
1984+
if (nc == it.second.end()) {
19861985
// ok to add all of them
1987-
constFunctions.splice(constFunctions.end(), it->second);
1986+
constFunctions.splice(constFunctions.end(), it.second);
19881987
}
19891988
}
19901989
}

lib/checkunusedvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ void Variables::readAliases(unsigned int varid, const Token* tok)
245245
VariableUsage *usage = find(varid);
246246

247247
if (usage) {
248-
for (std::set<unsigned int>::iterator aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
249-
VariableUsage *aliased = find(*aliases);
248+
for (unsigned int aliases : usage->_aliases) {
249+
VariableUsage *aliased = find(aliases);
250250

251251
if (aliased) {
252252
aliased->_read = true;

lib/cppcheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ static std::string executeAddon(const AddonInfo &addonInfo,
202202
pythonExe = cmdFileName(defaultPythonExe);
203203
else {
204204
#ifdef _WIN32
205-
const char *p[] = { "python3.exe", "python.exe" };
205+
const char *py_exes[] = { "python3.exe", "python.exe" };
206206
#else
207-
const char *p[] = { "python3", "python" };
207+
const char *py_exes[] = { "python3", "python" };
208208
#endif
209-
for (int i = 0; i < 2; ++i) {
209+
for (const char* py_exe : py_exes) {
210210
std::string out;
211-
if (executeCommand(p[i], split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) {
212-
pythonExe = p[i];
211+
if (executeCommand(py_exe, split("--version"), redirect, &out) && out.compare(0, 7, "Python ") == 0 && std::isdigit(out[7])) {
212+
pythonExe = py_exe;
213213
break;
214214
}
215215
}

0 commit comments

Comments
 (0)