tests/auto/*: port Q_FOREACH to ranged-for

The loop was iterating over a temporary container, so it couldn't have
changed it. Store the container in a const auto variable and use
ranged-for.

In files where Q_FOREACH isn't used any more, remove
"#undef QT_NO_FOREACH".

Task-number: QTBUG-115839
Change-Id: I402df5fa48f4287f3cc989ddae1524da43999049
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Ahmad Samir 2023-08-11 03:09:40 +03:00
parent c7a8e1e992
commit e7b07b64c7
9 changed files with 19 additions and 21 deletions

View File

@ -410,7 +410,8 @@ void tst_QFile::cleanup()
// Clean out everything except the readonly-files. // Clean out everything except the readonly-files.
const QDir dir(m_temporaryDir.path()); const QDir dir(m_temporaryDir.path());
foreach (const QFileInfo &fi, dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) { const auto entries = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
for (const QFileInfo &fi : entries) {
const QString fileName = fi.fileName(); const QString fileName = fi.fileName();
if (fileName != QLatin1String(noReadFile) && fileName != QLatin1String(readOnlyFile)) { if (fileName != QLatin1String(noReadFile) && fileName != QLatin1String(readOnlyFile)) {
const QString absoluteFilePath = fi.absoluteFilePath(); const QString absoluteFilePath = fi.absoluteFilePath();

View File

@ -1,8 +1,6 @@
// Copyright (C) 2021 The Qt Company Ltd. // Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <QTest> #include <QTest>
#include <QStandardPaths> #include <QStandardPaths>
#include <QScopeGuard> #include <QScopeGuard>
@ -1520,9 +1518,9 @@ void tst_QFileInfo::isHidden_data()
{ {
QTest::addColumn<QString>("path"); QTest::addColumn<QString>("path");
QTest::addColumn<bool>("isHidden"); QTest::addColumn<bool>("isHidden");
foreach (const QFileInfo& info, QDir::drives()) { const auto drives = QDir::drives();
for (const QFileInfo& info : drives)
QTest::newRow(qPrintable("drive." + info.path())) << info.path() << false; QTest::newRow(qPrintable("drive." + info.path())) << info.path() << false;
}
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
QVERIFY(QDir("./hidden-directory").exists() || QDir().mkdir("./hidden-directory")); QVERIFY(QDir("./hidden-directory").exists() || QDir().mkdir("./hidden-directory"));

View File

@ -1,8 +1,6 @@
// Copyright (C) 2013 BlackBerry Limited. All rights reserved. // Copyright (C) 2013 BlackBerry Limited. All rights reserved.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <QTest> #include <QTest>
#include <qplatformdefs.h> #include <qplatformdefs.h>
@ -76,7 +74,8 @@ void tst_QFileSelector::basicTest_data()
# if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || defined(Q_OS_FREEBSD) # if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || defined(Q_OS_FREEBSD)
distributionName = QSysInfo::productType(); distributionName = QSysInfo::productType();
# endif # endif
foreach (const QString &selector, QFileSelectorPrivate::platformSelectors()) { const auto platformSelectors = QFileSelectorPrivate::platformSelectors();
for (const QString &selector : platformSelectors) {
// skip the Linux distribution name (if any) since we don't have files for them // skip the Linux distribution name (if any) since we don't have files for them
if (selector == distributionName) if (selector == distributionName)
continue; continue;

View File

@ -739,7 +739,8 @@ public slots:
QCOMPARE(finfo.absolutePath(), moveSrcDir.absolutePath()); QCOMPARE(finfo.absolutePath(), moveSrcDir.absolutePath());
if (!added) { if (!added) {
foreach (const QFileInfo &fi, moveDestination.entryInfoList(QDir::Files | QDir::NoSymLinks)) const auto entries = moveDestination.entryInfoList(QDir::Files | QDir::NoSymLinks);
for (const QFileInfo &fi : entries)
watcher->addPath(fi.absoluteFilePath()); watcher->addPath(fi.absoluteFilePath());
added = true; added = true;
} }
@ -793,7 +794,7 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved()
QCOMPARE(changedSpy.count(), 0); QCOMPARE(changedSpy.count(), 0);
// move files to second directory // move files to second directory
foreach (const QFileInfo &finfo, files) for (const QFileInfo &finfo : files)
QVERIFY(testDir.rename(finfo.fileName(), QString("movehere/%2").arg(finfo.fileName()))); QVERIFY(testDir.rename(finfo.fileName(), QString("movehere/%2").arg(finfo.fileName())));
QCoreApplication::processEvents(); QCoreApplication::processEvents();

View File

@ -391,7 +391,8 @@ void tst_QTemporaryDir::openOnRootDrives()
#endif #endif
// If it's possible to create a file in the root directory, it // If it's possible to create a file in the root directory, it
// must be possible to create a temp dir there too. // must be possible to create a temp dir there too.
foreach (const QFileInfo &driveInfo, QDir::drives()) { const auto drives = QDir::drives();
for (const QFileInfo &driveInfo : drives) {
QFile testFile(driveInfo.filePath() + "XXXXXX"); QFile testFile(driveInfo.filePath() + "XXXXXX");
if (testFile.open(QIODevice::ReadWrite)) { if (testFile.open(QIODevice::ReadWrite)) {
testFile.remove(); testFile.remove();

View File

@ -524,7 +524,8 @@ void tst_QTemporaryFile::openOnRootDrives()
#endif #endif
// If it's possible to create a file in the root directory, it // If it's possible to create a file in the root directory, it
// must be possible to create a temp file there too. // must be possible to create a temp file there too.
foreach (QFileInfo driveInfo, QDir::drives()) { const auto drives = QDir::drives();
for (const QFileInfo &driveInfo : drives) {
QFile testFile(driveInfo.filePath() + "XXXXXX.txt"); QFile testFile(driveInfo.filePath() + "XXXXXX.txt");
if (testFile.open(QIODevice::ReadWrite)) { if (testFile.open(QIODevice::ReadWrite)) {
testFile.remove(); testFile.remove();

View File

@ -1,8 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <QTest> #include <QTest>
#include <QMimeData> #include <QMimeData>
@ -312,7 +310,8 @@ void tst_QMimeData::setUrls() const
QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n")); QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n"));
// test and verify that setData doesn't corrupt url content // test and verify that setData doesn't corrupt url content
foreach (const QString &format, mimeData.formats()) { const auto allFormats = mimeData.formats();
for (const QString &format : allFormats) {
QVariant before = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray)); QVariant before = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray));
mimeData.setData(format, mimeData.data(format)); mimeData.setData(format, mimeData.data(format));
QVariant after = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray)); QVariant after = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray));

View File

@ -3,8 +3,6 @@
// Copyright (C) 2016 Intel Corporation. // Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <qvariant.h> #include <qvariant.h>
// don't assume <type_traits> // don't assume <type_traits>
@ -4375,7 +4373,8 @@ void tst_QVariant::dataStream_data(QDataStream::Version version)
path = path.prepend(":/stream/").append("/"); path = path.prepend(":/stream/").append("/");
QDir dir(path); QDir dir(path);
uint i = 0; uint i = 0;
foreach (const QFileInfo &fileInfo, dir.entryInfoList(QStringList() << "*.bin")) { const auto entries = dir.entryInfoList(QStringList{u"*.bin"_s});
for (const QFileInfo &fileInfo : entries) {
QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath(); QTest::newRow((path + fileInfo.fileName()).toLatin1()) << fileInfo.filePath();
i += 1; i += 1;
} }

View File

@ -1,8 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <QTest> #include <QTest>
#include <QtXml/QtXml> #include <QtXml/QtXml>
#include <QtGui/QFontInfo> #include <QtGui/QFontInfo>
@ -72,7 +70,8 @@ void tst_QCssParser::scanner_data()
#endif #endif
d.cd("testdata"); d.cd("testdata");
d.cd("scanner"); d.cd("scanner");
foreach (QFileInfo test, d.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) { const auto entries = d.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const QFileInfo &test : entries) {
QString dir = test.absoluteFilePath() + QDir::separator(); QString dir = test.absoluteFilePath() + QDir::separator();
QTest::newRow(qPrintable(test.baseName())) QTest::newRow(qPrintable(test.baseName()))
<< dir + "input" << dir + "input"