tests/auto/: port Q_FOREACH to ranged-for, local const containers

These are local containers that are either:
- Already const and didn't need Q_FOREACH to begin with
- Can be simply made const, just by adding const keyword

In one case the unittest checked that the container's size is 1, so use
list.first() instead of a for-loop.

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

Drive-by changes:
- Remove parenthesis from one-line for-loops
- Make the for-loop variable a const& where a copy isn't needed

Task-number: QTBUG-115839
Change-Id: Ide34122b9cda798b80c4ca9d2d5af76024bc7a92
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Ahmad Samir 2023-08-11 17:20:24 +03:00
parent e7b07b64c7
commit 8e0281a8be
10 changed files with 27 additions and 47 deletions

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 <QCoreApplication> #include <QCoreApplication>
@ -780,9 +778,9 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved()
QVERIFY(watcher.addPath(movePath)); QVERIFY(watcher.addPath(movePath));
// add files to watcher // add files to watcher
QFileInfoList files = testDir.entryInfoList(QDir::Files | QDir::NoSymLinks); const QFileInfoList files = testDir.entryInfoList(QDir::Files | QDir::NoSymLinks);
QCOMPARE(files.size(), fileCount); QCOMPARE(files.size(), fileCount);
foreach (const QFileInfo &finfo, files) for (const QFileInfo &finfo : files)
QVERIFY(watcher.addPath(finfo.absoluteFilePath())); QVERIFY(watcher.addPath(finfo.absoluteFilePath()));
// create the signal receiver // create the signal receiver

View File

@ -2,8 +2,6 @@
// Copyright (C) 2020 Intel Corporation. // Copyright (C) 2020 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 <qstandardpaths.h> #include <qstandardpaths.h>
#include <QTest> #include <QTest>
#include <QOperatingSystemVersion> #include <QOperatingSystemVersion>
@ -409,7 +407,7 @@ static inline QFileInfo findSh()
QByteArray pEnv = qgetenv("PATH"); QByteArray pEnv = qgetenv("PATH");
const QLatin1Char pathSep(':'); const QLatin1Char pathSep(':');
const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, Qt::SkipEmptyParts); const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, Qt::SkipEmptyParts);
foreach (const QString &path, rawPaths) { for (const QString &path : rawPaths) {
if (QFile::exists(path + sh)) if (QFile::exists(path + sh))
return QFileInfo(path + sh); return QFileInfo(path + sh);
} }

View File

@ -107,8 +107,8 @@ void tst_QArrayData::simpleVector()
SimpleVector<int> v4(nullptr, data, 0); SimpleVector<int> v4(nullptr, data, 0);
SimpleVector<int> v5(nullptr, data, 1); SimpleVector<int> v5(nullptr, data, 1);
SimpleVector<int> v6(nullptr, data, 7); SimpleVector<int> v6(nullptr, data, 7);
SimpleVector<int> v7(10, 5); const SimpleVector<int> v7(10, 5);
SimpleVector<int> v8(array, array + sizeof(array)/sizeof(*array)); const SimpleVector<int> v8(array, array + sizeof(array)/sizeof(*array));
v3 = v1; v3 = v1;
v1.swap(v3); v1.swap(v3);
@ -236,7 +236,7 @@ void tst_QArrayData::simpleVector()
{ {
int count = 0; int count = 0;
Q_FOREACH (int value, v7) { for (int value : v7) {
QCOMPARE(value, 5); QCOMPARE(value, 5);
++count; ++count;
} }
@ -246,7 +246,7 @@ void tst_QArrayData::simpleVector()
{ {
int count = 0; int count = 0;
Q_FOREACH (int value, v8) { for (int value : v8) {
QCOMPARE(value, count); QCOMPARE(value, count);
++count; ++count;
} }

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 <qeasingcurve.h> #include <qeasingcurve.h>
@ -576,10 +574,10 @@ void tst_QEasingCurve::bezierSpline_data()
static inline void setupBezierSpline(QEasingCurve *easingCurve, const QString &string) static inline void setupBezierSpline(QEasingCurve *easingCurve, const QString &string)
{ {
QStringList pointStr = string.split(QLatin1Char(' ')); const QStringList pointStr = string.split(QLatin1Char(' '));
QList<QPointF> points; QList<QPointF> points;
foreach (const QString &str, pointStr) { for (const QString &str : pointStr) {
QStringList coordStr = str.split(QLatin1Char(',')); QStringList coordStr = str.split(QLatin1Char(','));
QPointF point(coordStr.first().toDouble(), coordStr.last().toDouble()); QPointF point(coordStr.first().toDouble(), coordStr.last().toDouble());
points.append(point); points.append(point);
@ -644,9 +642,9 @@ void tst_QEasingCurve::tcbSpline_data()
static inline void setupTCBSpline(QEasingCurve *easingCurve, const QString &string) static inline void setupTCBSpline(QEasingCurve *easingCurve, const QString &string)
{ {
QStringList pointStr = string.split(QLatin1Char(' ')); const QStringList pointStr = string.split(QLatin1Char(' '));
foreach (const QString &str, pointStr) { for (const QString &str : pointStr) {
QStringList coordStr = str.split(QLatin1Char(',')); QStringList coordStr = str.split(QLatin1Char(','));
Q_ASSERT(coordStr.size() == 5); Q_ASSERT(coordStr.size() == 5);
QPointF point(coordStr.first().toDouble(), coordStr.at(1).toDouble()); QPointF point(coordStr.first().toDouble(), coordStr.at(1).toDouble());

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 <QAtomicInt> #include <QAtomicInt>
#include <QThread> #include <QThread>
@ -562,25 +560,22 @@ void tst_QList::constructors_reserveAndInitialize() const
{ {
// default-initialise items // default-initialise items
QList<int> myInt(5, 42); const QList<int> myInt(5, 42);
QVERIFY(myInt.capacity() == 5); QVERIFY(myInt.capacity() == 5);
foreach (int meaningoflife, myInt) { for (int meaningoflife : myInt)
QCOMPARE(meaningoflife, 42); QCOMPARE(meaningoflife, 42);
}
QList<QString> myString(5, QString::fromLatin1("c++")); const QList<QString> myString(5, QString::fromLatin1("c++"));
QVERIFY(myString.capacity() == 5); QVERIFY(myString.capacity() == 5);
// make sure all items are initialised ok // make sure all items are initialised ok
foreach (QString meaningoflife, myString) { for (const QString &meaningoflife : myString)
QCOMPARE(meaningoflife, QString::fromLatin1("c++")); QCOMPARE(meaningoflife, QString::fromLatin1("c++"));
}
QList<Custom> myCustom(5, Custom('n')); const QList<Custom> myCustom(5, Custom('n'));
QVERIFY(myCustom.capacity() == 5); QVERIFY(myCustom.capacity() == 5);
// make sure all items are initialised ok // make sure all items are initialised ok
foreach (Custom meaningoflife, myCustom) { for (Custom meaningoflife : myCustom)
QCOMPARE(meaningoflife.i, 'n'); QCOMPARE(meaningoflife.i, 'n');
}
} }
template<typename T> template<typename T>

View File

@ -9,8 +9,6 @@ qt_internal_add_test(tst_qdbusmarshall
OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../" OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../"
SOURCES SOURCES
../tst_qdbusmarshall.cpp ../tst_qdbusmarshall.cpp
NO_PCH_SOURCES
../tst_qdbusmarshall.cpp # undef QT_NO_FOREACH
LIBRARIES LIBRARIES
Qt::CorePrivate Qt::CorePrivate
Qt::DBusPrivate Qt::DBusPrivate

View File

@ -2,8 +2,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 <QtCore/QtCore> #include <QtCore/QtCore>
#include <QTest> #include <QTest>
#include <QTestEventLoop> #include <QTestEventLoop>
@ -1310,7 +1308,7 @@ void tst_QDBusMarshall::demarshallStrings_data()
<< ValSigPair(QVariant::fromValue(QString()), 's') << ValSigPair(QVariant::fromValue(QString()), 's')
<< ValSigPair(QVariant::fromValue(QDBusObjectPath()), 'o') << ValSigPair(QVariant::fromValue(QDBusObjectPath()), 'o')
<< ValSigPair(QVariant::fromValue(QDBusSignature()), 'g'); << ValSigPair(QVariant::fromValue(QDBusSignature()), 'g');
foreach (ValSigPair valSigPair, nullStringTypes) { for (const ValSigPair &valSigPair : nullStringTypes) {
QTest::newRow("bool(false)") << QVariant(false) << valSigPair.second << valSigPair.first; QTest::newRow("bool(false)") << QVariant(false) << valSigPair.second << valSigPair.first;
QTest::newRow("bool(true)") << QVariant(true) << valSigPair.second << valSigPair.first; QTest::newRow("bool(true)") << QVariant(true) << valSigPair.second << valSigPair.first;
QTest::newRow("byte") << QVariant::fromValue(uchar(1)) << valSigPair.second << valSigPair.first; QTest::newRow("byte") << QVariant::fromValue(uchar(1)) << valSigPair.second << valSigPair.first;

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 <QCoreApplication> #include <QCoreApplication>
#include <QDomDocument> #include <QDomDocument>
#include <QMetaType> #include <QMetaType>
@ -110,7 +108,7 @@ void tst_QDBusXmlParser::parsing_data()
void tst_QDBusXmlParser::parsing_common(const QString &xmlData) void tst_QDBusXmlParser::parsing_common(const QString &xmlData)
{ {
QDBusIntrospection::Object obj = const QDBusIntrospection::Object obj =
QDBusIntrospection::parseObject(xmlData, "local.testing", "/"); QDBusIntrospection::parseObject(xmlData, "local.testing", "/");
QFETCH(int, interfaceCount); QFETCH(int, interfaceCount);
QFETCH(int, objectCount); QFETCH(int, objectCount);
@ -124,7 +122,7 @@ void tst_QDBusXmlParser::parsing_common(const QString &xmlData)
// also verify the naming // also verify the naming
int i = 0; int i = 0;
foreach (QString name, obj.interfaces) { for (const QString &name : obj.interfaces) {
const QString expectedName = QString("iface.iface%1").arg(i+1); const QString expectedName = QString("iface.iface%1").arg(i+1);
QCOMPARE(name, expectedName); QCOMPARE(name, expectedName);
@ -134,7 +132,7 @@ void tst_QDBusXmlParser::parsing_common(const QString &xmlData)
} }
i = 0; i = 0;
foreach (QString name, obj.childObjects) for (const QString &name : obj.childObjects)
QCOMPARE(name, QString("obj%1").arg(++i)); QCOMPARE(name, QString("obj%1").arg(++i));
} }

View File

@ -3786,10 +3786,10 @@ static QLinearGradient inverseGradient(QLinearGradient g)
{ {
QLinearGradient g2 = g; QLinearGradient g2 = g;
QGradientStops stops = g.stops(); const QGradientStops stops = g.stops();
QGradientStops inverse; QGradientStops inverse;
foreach (QGradientStop stop, stops) for (const QGradientStop &stop : stops)
inverse << QGradientStop(1 - stop.first, stop.second); inverse << QGradientStop(1 - stop.first, stop.second);
g2.setStops(inverse); g2.setStops(inverse);

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 <qglyphrun.h> #include <qglyphrun.h>
@ -410,7 +408,7 @@ void tst_QGlyphRun::drawMultiScriptText1()
QPixmap drawGlyphs(1000, 1000); QPixmap drawGlyphs(1000, 1000);
drawGlyphs.fill(Qt::white); drawGlyphs.fill(Qt::white);
QList<QGlyphRun> glyphsList = textLayout.glyphRuns(); const QList<QGlyphRun> glyphsList = textLayout.glyphRuns();
QCOMPARE(glyphsList.size(), 1); QCOMPARE(glyphsList.size(), 1);
{ {
@ -420,8 +418,7 @@ void tst_QGlyphRun::drawMultiScriptText1()
{ {
QPainter p(&drawGlyphs); QPainter p(&drawGlyphs);
foreach (QGlyphRun glyphs, glyphsList) p.drawGlyphRun(QPointF(50, 50), glyphsList.first());
p.drawGlyphRun(QPointF(50, 50), glyphs);
} }
#if defined(DEBUG_SAVE_IMAGE) #if defined(DEBUG_SAVE_IMAGE)
@ -451,7 +448,7 @@ void tst_QGlyphRun::drawMultiScriptText2()
QPixmap drawGlyphs(1000, 1000); QPixmap drawGlyphs(1000, 1000);
drawGlyphs.fill(Qt::white); drawGlyphs.fill(Qt::white);
QList<QGlyphRun> glyphsList = textLayout.glyphRuns(); const QList<QGlyphRun> glyphsList = textLayout.glyphRuns();
QCOMPARE(glyphsList.size(), 2); QCOMPARE(glyphsList.size(), 2);
{ {
@ -461,7 +458,7 @@ void tst_QGlyphRun::drawMultiScriptText2()
{ {
QPainter p(&drawGlyphs); QPainter p(&drawGlyphs);
foreach (QGlyphRun glyphs, glyphsList) for (const QGlyphRun &glyphs : glyphsList)
p.drawGlyphRun(QPointF(50, 50), glyphs); p.drawGlyphRun(QPointF(50, 50), glyphs);
} }