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.
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();
if (fileName != QLatin1String(noReadFile) && fileName != QLatin1String(readOnlyFile)) {
const QString absoluteFilePath = fi.absoluteFilePath();

View File

@ -1,8 +1,6 @@
// Copyright (C) 2021 The Qt Company Ltd.
// 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 <QStandardPaths>
#include <QScopeGuard>
@ -1520,9 +1518,9 @@ void tst_QFileInfo::isHidden_data()
{
QTest::addColumn<QString>("path");
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;
}
#if defined(Q_OS_WIN)
QVERIFY(QDir("./hidden-directory").exists() || QDir().mkdir("./hidden-directory"));

View File

@ -1,8 +1,6 @@
// 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
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
#include <QTest>
#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)
distributionName = QSysInfo::productType();
# 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
if (selector == distributionName)
continue;

View File

@ -739,7 +739,8 @@ public slots:
QCOMPARE(finfo.absolutePath(), moveSrcDir.absolutePath());
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());
added = true;
}
@ -793,7 +794,7 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved()
QCOMPARE(changedSpy.count(), 0);
// 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())));
QCoreApplication::processEvents();

View File

@ -391,7 +391,8 @@ void tst_QTemporaryDir::openOnRootDrives()
#endif
// If it's possible to create a file in the root directory, it
// 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");
if (testFile.open(QIODevice::ReadWrite)) {
testFile.remove();

View File

@ -524,7 +524,8 @@ void tst_QTemporaryFile::openOnRootDrives()
#endif
// If it's possible to create a file in the root directory, it
// 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");
if (testFile.open(QIODevice::ReadWrite)) {
testFile.remove();

View File

@ -1,8 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// 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 <QMimeData>
@ -312,7 +310,8 @@ void tst_QMimeData::setUrls() const
QCOMPARE(mimeData.text(), QString("http://qt-project.org\nhttp://www.google.com\n"));
// 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));
mimeData.setData(format, mimeData.data(format));
QVariant after = mimeData.retrieveData(format, QMetaType(QMetaType::QByteArray));

View File

@ -3,8 +3,6 @@
// Copyright (C) 2016 Intel Corporation.
// 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>
// don't assume <type_traits>
@ -4375,7 +4373,8 @@ void tst_QVariant::dataStream_data(QDataStream::Version version)
path = path.prepend(":/stream/").append("/");
QDir dir(path);
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();
i += 1;
}

View File

@ -1,8 +1,6 @@
// Copyright (C) 2016 The Qt Company Ltd.
// 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 <QtXml/QtXml>
#include <QtGui/QFontInfo>
@ -72,7 +70,8 @@ void tst_QCssParser::scanner_data()
#endif
d.cd("testdata");
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();
QTest::newRow(qPrintable(test.baseName()))
<< dir + "input"