From e7b07b64c7b0536eccba6ff25e23badb88226813 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 11 Aug 2023 03:09:40 +0300 Subject: [PATCH] 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 --- tests/auto/corelib/io/qfile/tst_qfile.cpp | 3 ++- tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 6 ++---- tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp | 5 ++--- .../io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp | 5 +++-- tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp | 3 ++- tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp | 3 ++- tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp | 5 ++--- tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 5 ++--- tests/auto/gui/text/qcssparser/tst_qcssparser.cpp | 5 ++--- 9 files changed, 19 insertions(+), 21 deletions(-) diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp index 578ff7f89f..313235fa11 100644 --- a/tests/auto/corelib/io/qfile/tst_qfile.cpp +++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp @@ -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(); diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index 97bada7668..e13b16260e 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -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 #include #include @@ -1520,9 +1518,9 @@ void tst_QFileInfo::isHidden_data() { QTest::addColumn("path"); QTest::addColumn("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")); diff --git a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp index 3c17b6eb57..34d9076ff7 100644 --- a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp +++ b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp @@ -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 #include @@ -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; diff --git a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp index 057f37d1a9..fc52ea6692 100644 --- a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp +++ b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp @@ -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(); diff --git a/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp b/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp index bd371f1e0f..2db1f1f98b 100644 --- a/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp +++ b/tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp @@ -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(); diff --git a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp index 6f4c99cf21..876931c239 100644 --- a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp @@ -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(); diff --git a/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp b/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp index 2174638abd..30badb18b1 100644 --- a/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp +++ b/tests/auto/corelib/kernel/qmimedata/tst_qmimedata.cpp @@ -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 #include @@ -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)); diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index baae759dd1..3a621b91d6 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -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 // don't assume @@ -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; } diff --git a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp index 83bc18db66..119a4edd8d 100644 --- a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp +++ b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp @@ -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 #include #include @@ -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"