tests: port assorted trivial uses of Q_FOREACH to ranged for loops

All of these fall into the trivial category: loops over (readily made)
const local containers. As such, they cannot possibly depend on the
safety copy that Q_FOREACH performs, so are safe to port as-is to
ranged for loops.

There may be more where these came from, but these were the ones that
stood out as immediately obvious when scanning the 100s of uses in
qtbase, so I preferred to directly fix them over white-listing their
files with QT_NO_FOREACH (which still may be necessary for some files,
as this patch may not port all uses in that file).

Pick-to: 6.6 6.5
Task-nubmber: QTBUG-115839
Change-Id: I7b7893bec8254f902660dac24167113aca855029
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Marc Mutz 2023-08-08 10:54:07 +02:00 committed by Ahmad Samir
parent 47375a213f
commit f2f8820073
18 changed files with 43 additions and 42 deletions

View File

@ -876,8 +876,8 @@ void tst_QPluginLoader::loadMachO_data()
QTest::newRow("machtest/good.fat.stub-i386.dylib") << false;
QDir d(QFINDTESTDATA("machtest"));
QStringList badlist = d.entryList(QStringList() << "bad*.dylib");
foreach (const QString &bad, badlist)
const QStringList badlist = d.entryList(QStringList() << "bad*.dylib");
for (const QString &bad : badlist)
QTest::newRow(qPrintable("machtest/" + bad)) << false;
#endif
}

View File

@ -1028,11 +1028,12 @@ void tst_QFileSystemModel::drives()
QFileSystemModel model;
model.setRootPath(path);
model.fetchMore(QModelIndex());
QFileInfoList drives = QDir::drives();
const QFileInfoList drives = QDir::drives();
int driveCount = 0;
foreach(const QFileInfo& driveRoot, drives)
for (const QFileInfo& driveRoot : drives) {
if (driveRoot.exists())
driveCount++;
}
QTRY_COMPARE(model.rowCount(), driveCount);
}

View File

@ -319,8 +319,8 @@ void tst_QFontDatabase::fallbackFonts()
layout.createLine();
layout.endLayout();
QList<QGlyphRun> runs = layout.glyphRuns(0, 1);
foreach (QGlyphRun run, runs) {
const QList<QGlyphRun> runs = layout.glyphRuns(0, 1);
for (QGlyphRun run : runs) {
QRawFont rawFont = run.rawFont();
QVERIFY(rawFont.isValid());

View File

@ -490,7 +490,7 @@ void tst_QRawFont::supportedWritingSystems_data()
void tst_QRawFont::supportedWritingSystems()
{
QFETCH(QString, fileName);
QFETCH(WritingSystemList, writingSystems);
QFETCH(const WritingSystemList, writingSystems);
QFETCH(QFont::HintingPreference, hintingPreference);
QRawFont font(fileName, 10, hintingPreference);
@ -499,7 +499,7 @@ void tst_QRawFont::supportedWritingSystems()
WritingSystemList actualWritingSystems = font.supportedWritingSystems();
QCOMPARE(actualWritingSystems.size(), writingSystems.size());
foreach (QFontDatabase::WritingSystem writingSystem, writingSystems)
for (QFontDatabase::WritingSystem writingSystem : writingSystems)
QVERIFY(actualWritingSystems.contains(writingSystem));
}

View File

@ -166,11 +166,11 @@ void tst_QNetworkRequest::rawHeaderList_data()
void tst_QNetworkRequest::rawHeaderList()
{
QFETCH(QList<QByteArray>, set);
QFETCH(const QList<QByteArray>, set);
QFETCH(QList<QByteArray>, expected);
QNetworkRequest request;
foreach (QByteArray header, set)
for (const QByteArray &header : set)
request.setRawHeader(header, "a value");
QList<QByteArray> got = request.rawHeaderList();

View File

@ -190,9 +190,9 @@ QString tst_QDnsLookup::domainName(const QString &input)
QString tst_QDnsLookup::domainNameList(const QString &input)
{
QStringList list = input.split(QLatin1Char(';'));
const QStringList list = input.split(QLatin1Char(';'));
QString result;
foreach (const QString &s, list) {
for (const QString &s : list) {
if (!result.isEmpty())
result += ';';
result += domainName(s);

View File

@ -71,8 +71,8 @@ void tst_QNetworkInterface::initTestCase()
void tst_QNetworkInterface::dump()
{
// This is for manual testing:
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
foreach (const QNetworkInterface &i, allInterfaces) {
const QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &i : allInterfaces) {
QString flags;
if (i.flags() & QNetworkInterface::IsUp) flags += "Up,";
if (i.flags() & QNetworkInterface::IsRunning) flags += "Running,";

View File

@ -487,8 +487,8 @@ void tst_QTcpSocket::bind_data()
bool testIpv6 = false;
// iterate all interfaces, add all addresses on them as test data
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
foreach (const QNetworkInterface &netinterface, interfaces) {
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &netinterface : interfaces) {
if (!netinterface.isValid())
continue;

View File

@ -138,13 +138,13 @@ void tst_QSslCertificate::initTestCase()
testDataDir += QLatin1String("/");
QDir dir(testDataDir + "certificates");
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable);
const QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable);
QRegularExpression rxCert(QLatin1String("^.+\\.(pem|der)$"));
QRegularExpression rxSan(QLatin1String("^(.+\\.(?:pem|der))\\.san$"));
QRegularExpression rxPubKey(QLatin1String("^(.+\\.(?:pem|der))\\.pubkey$"));
QRegularExpression rxDigest(QLatin1String("^(.+\\.(?:pem|der))\\.digest-(md5|sha1)$"));
QRegularExpressionMatch match;
foreach (QFileInfo fileInfo, fileInfoList) {
for (const QFileInfo &fileInfo : fileInfoList) {
if ((match = rxCert.match(fileInfo.fileName())).hasMatch())
certInfoList <<
CertInfo(fileInfo,

View File

@ -340,11 +340,11 @@ void tst_QPrinterInfo::testAssignment()
void tst_QPrinterInfo::namedPrinter()
{
QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
const QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
QStringList printerNames;
foreach (const QPrinterInfo &pi, printers) {
for (const QPrinterInfo &pi : printers) {
QPrinterInfo pi2 = QPrinterInfo::printerInfo(pi.printerName());
QCOMPARE(pi2.printerName(), pi.printerName());
QCOMPARE(pi2.description(), pi.description());

View File

@ -199,7 +199,7 @@ void runVerifyDeployment(const QString &name)
const QList<QString> parts = QString::fromLocal8Bit(libraries).split("dyld: loaded:");
const QString qtPath = QLibraryInfo::path(QLibraryInfo::PrefixPath);
// Let assume Qt is not installed in system
foreach (QString part, parts) {
for (const QString &part : parts) {
part = part.trimmed();
if (part.isEmpty())
continue;

View File

@ -745,8 +745,8 @@ void PaintCommands::runCommand(const QString &scriptLine)
return;
}
QString firstWord = scriptLine.section(separators, 0, 0);
QList<int> indices = s_commandHash.values(firstWord);
foreach(int idx, indices) {
const QList<int> indices = s_commandHash.values(firstWord);
for (int idx : indices) {
PaintCommandInfos command = s_commandInfoTable.at(idx);
Q_ASSERT(command.regExp.isValid());
QRegularExpressionMatch match = command.regExp.match(scriptLine);

View File

@ -143,8 +143,8 @@ MainWindow::MainWindow()
void MainWindow::setDirectory(const QString &path)
{
QDir dir(path);
QStringList files = dir.entryList(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
foreach(const QString &file, files) {
const QStringList files = dir.entryList(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
for (const QString &file : files) {
QImageReader img(path + QLatin1Char('/') +file);
QImage image = img.read();
if (!image.isNull()) {

View File

@ -84,8 +84,8 @@ void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasText()) {
const QMimeData *mime = event->mimeData();
QStringList pieces = mime->text().split(QRegularExpression("\\s+"),
Qt::SkipEmptyParts);
const QStringList pieces = mime->text().split(QRegularExpression("\\s+"),
Qt::SkipEmptyParts);
QPoint position = event->pos();
QPoint hotSpot;
@ -98,7 +98,7 @@ void DragWidget::dropEvent(QDropEvent *event)
dropTimer.start(500, this);
update();
foreach (QString piece, pieces) {
for (const QString &piece : pieces) {
FramedLabel *newLabel = new FramedLabel(piece, this);
newLabel->move(position - hotSpot);
newLabel->show();

View File

@ -9,10 +9,10 @@
QList<CustomGroup*> CustomScene::selectedCustomGroups() const
{
QList<QGraphicsItem*> all = selectedItems();
const QList<QGraphicsItem*> all = selectedItems();
QList<CustomGroup*> groups;
foreach (QGraphicsItem *item, all) {
for (QGraphicsItem *item : all) {
CustomGroup* group = qgraphicsitem_cast<CustomGroup*>(item);
if (group)
groups.append(group);
@ -23,10 +23,10 @@ QList<CustomGroup*> CustomScene::selectedCustomGroups() const
QList<CustomItem*> CustomScene::selectedCustomItems() const
{
QList<QGraphicsItem*> all = selectedItems();
const QList<QGraphicsItem*> all = selectedItems();
QList<CustomItem*> items;
foreach (QGraphicsItem *item, all) {
for (QGraphicsItem *item : all) {
CustomItem* citem = qgraphicsitem_cast<CustomItem*>(item);
if (citem)
items.append(citem);

View File

@ -95,15 +95,15 @@ void Widget::on_scaleItem_valueChanged(int value)
void Widget::on_group_clicked()
{
QList<QGraphicsItem*> all = scene->selectedItems();
const QList<QGraphicsItem*> all = scene->selectedItems();
if (all.size() < 2)
return;
QList<CustomItem*> items = scene->selectedCustomItems();
const QList<CustomItem*> items = scene->selectedCustomItems();
QList<CustomGroup*> groups = scene->selectedCustomGroups();
if (groups.size() == 1) {
foreach (CustomItem *item, items) {
for (CustomItem *item : items) {
item->setSelected(false);
groups[0]->addToGroup(item);
}
@ -113,7 +113,7 @@ void Widget::on_group_clicked()
CustomGroup* group = new CustomGroup;
scene->addItem(group);
foreach (QGraphicsItem *item, all) {
for (QGraphicsItem *item : all) {
item->setSelected(false);
group->addToGroup(item);
}
@ -124,9 +124,9 @@ void Widget::on_group_clicked()
void Widget::on_dismantle_clicked()
{
QList<CustomGroup*> groups = scene->selectedCustomGroups();
const QList<CustomGroup*> groups = scene->selectedCustomGroups();
foreach (CustomGroup *group, groups) {
for (CustomGroup *group : groups) {
foreach (QGraphicsItem *item, group->childItems())
group->removeFromGroup(item);

View File

@ -16,8 +16,8 @@ Window::Window()
localeCombo->addItem("System", QLocale::system());
QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyTerritory);
foreach (const QLocale &locale, locales) {
const QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyTerritory);
for (const QLocale &locale : locales) {
QString label = QLocale::languageToString(locale.language());
label += QLatin1Char('/');
if (locale.script() != QLocale::AnyScript) {

View File

@ -85,8 +85,8 @@ void PropertyWatcher::setSubject(QObject *s, const QString &annotation)
void PropertyWatcher::updateAllFields()
{
QList<PropertyField *> fields = findChildren<PropertyField*>();
foreach (PropertyField *field, fields)
const QList<PropertyField *> fields = findChildren<PropertyField*>();
for (PropertyField *field : fields)
field->propertyChanged();
emit updatedAllFields(this);
}