From 66f06a930da0114f62a5470e778182c50117ad27 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 11 Dec 2019 12:47:39 +0100 Subject: [PATCH] Make QLocale(QString) explicit We should not implicitly convert a QString to a QLocale object. It can easily create unwanted side effects. Change-Id: I7bd9b4a4e4512c0e60176ee4d241d172f00fdc32 Reviewed-by: Edward Welbourne --- src/corelib/text/qlocale.h | 2 +- src/corelib/time/qtimezoneprivate_icu.cpp | 4 ++-- .../corelib/serialization/qtextstream/tst_qtextstream.cpp | 4 ++-- tests/auto/corelib/text/qcollator/tst_qcollator.cpp | 2 +- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 3 ++- tests/auto/corelib/text/qstring/tst_qstring.cpp | 4 ++-- tests/auto/corelib/text/qstringref/tst_qstringref.cpp | 2 +- tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp | 8 ++++---- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h index 3f1a8f2679..ca2b5a007f 100644 --- a/src/corelib/text/qlocale.h +++ b/src/corelib/text/qlocale.h @@ -941,7 +941,7 @@ public: Q_FLAG(DataSizeFormats) QLocale(); - QLocale(const QString &name); + explicit QLocale(const QString &name); QLocale(Language language, Country country = AnyCountry); QLocale(Language language, Script script, Country country); QLocale(const QLocale &other); diff --git a/src/corelib/time/qtimezoneprivate_icu.cpp b/src/corelib/time/qtimezoneprivate_icu.cpp index 8a92bbb387..33518994d1 100644 --- a/src/corelib/time/qtimezoneprivate_icu.cpp +++ b/src/corelib/time/qtimezoneprivate_icu.cpp @@ -347,9 +347,9 @@ QString QIcuTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const { // TODO No ICU API, use short name instead if (isDaylightTime(atMSecsSinceEpoch)) - return displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, QString()); + return displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, QLocale()); else - return displayName(QTimeZone::StandardTime, QTimeZone::ShortName, QString()); + return displayName(QTimeZone::StandardTime, QTimeZone::ShortName, QLocale()); } int QIcuTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const diff --git a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index 6565924f58..58f0d65f94 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -3022,7 +3022,7 @@ void tst_QTextStream::int_read_with_locale() QFETCH(int, output); QTextStream stream(&input); - stream.setLocale(locale); + stream.setLocale(QLocale(locale)); int result; stream >> result; QCOMPARE(result, output); @@ -3053,7 +3053,7 @@ void tst_QTextStream::int_write_with_locale() QString result; QTextStream stream(&result); - stream.setLocale(locale); + stream.setLocale(QLocale(locale)); if (numberFlags) stream.setNumberFlags(QTextStream::NumberFlags(numberFlags)); stream << input; diff --git a/tests/auto/corelib/text/qcollator/tst_qcollator.cpp b/tests/auto/corelib/text/qcollator/tst_qcollator.cpp index 6806aa09b5..439f80e795 100644 --- a/tests/auto/corelib/text/qcollator/tst_qcollator.cpp +++ b/tests/auto/corelib/text/qcollator/tst_qcollator.cpp @@ -193,7 +193,7 @@ void tst_QCollator::compare() QFETCH(bool, ignorePunctuation); QFETCH(int, punctuationResult); - QCollator collator(locale); + QCollator collator((QLocale(locale))); // Need to canonicalize sign to -1, 0 or 1, as .compare() can produce any -ve for <, any +ve for >. auto asSign = [](int compared) { return compared < 0 ? -1 : compared > 0 ? 1 : 0; diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 1a882733a3..6ed06ca237 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -3010,7 +3010,8 @@ void tst_QLocale::systemLocale() QFETCH(QLocale::Language, language); { - MySystemLocale sLocale(name); + QLocale locale(name); + MySystemLocale sLocale(locale); QCOMPARE(QLocale().language(), language); QCOMPARE(QLocale::system().language(), language); } diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 65f88c0e4d..bfecbf2b4b 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -4776,7 +4776,7 @@ void tst_QString::arg() is all messed up, because Qt Test itself uses QString::arg(). */ - TransientDefaultLocale transient(QString("de_DE")); + TransientDefaultLocale transient(QLocale(QString("de_DE"))); QString s4( "[%0]" ); QString s5( "[%1]" ); @@ -4924,7 +4924,7 @@ void tst_QString::arg() QCOMPARE(QString("%1").arg(-1., 3, 'g', -1, QChar('x')), QLatin1String("x-1")); QCOMPARE(QString("%1").arg(-100., 3, 'g', -1, QChar('x')), QLatin1String("-100")); - transient.revise(QString("ar")); + transient.revise(QLocale(QString("ar"))); QCOMPARE( QString("%L1").arg(12345.6789, 10, 'g', 7, QLatin1Char('0')), QString::fromUtf8("\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xac\xd9\xa3\xd9\xa4\xd9\xa5\xd9\xab\xd9\xa6\xd9\xa8") ); // "٠١٢٬٣٤٥٫٦٨" QCOMPARE( QString("%L1").arg(123456789, 13, 10, QLatin1Char('0')), diff --git a/tests/auto/corelib/text/qstringref/tst_qstringref.cpp b/tests/auto/corelib/text/qstringref/tst_qstringref.cpp index e1fa32f568..f8049ffabc 100644 --- a/tests/auto/corelib/text/qstringref/tst_qstringref.cpp +++ b/tests/auto/corelib/text/qstringref/tst_qstringref.cpp @@ -165,7 +165,7 @@ static inline double nan() void tst_QStringRef::cleanup() { - QLocale::setDefault(QString(QLatin1Char('C'))); + QLocale::setDefault(QLocale(QString(QLatin1Char('C')))); } void tst_QStringRef::at() diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index 332a00efb5..5c2af3b115 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -344,9 +344,9 @@ void tst_QTimeZone::dataStreamTest() QCOMPARE(tz2.comment(), QString("Qt Testing")); QCOMPARE(tz2.country(), QLocale::Norway); QCOMPARE(tz2.abbreviation(QDateTime::currentDateTime()), QString("QST")); - QCOMPARE(tz2.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()), + QCOMPARE(tz2.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QLocale()), QString("Qt Standard Time")); - QCOMPARE(tz2.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, QString()), + QCOMPARE(tz2.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, QLocale()), QString("Qt Standard Time")); QCOMPARE(tz2.offsetFromUtc(QDateTime::currentDateTime()), 123456); @@ -944,7 +944,7 @@ void tst_QTimeZone::utcTest() QCOMPARE(tzp.id(), QByteArray("UTC")); QCOMPARE(tzp.country(), QLocale::AnyCountry); QCOMPARE(tzp.abbreviation(0), QString("UTC")); - QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()), QString("UTC")); + QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QLocale()), QString("UTC")); QCOMPARE(tzp.offsetFromUtc(0), 0); QCOMPARE(tzp.standardTimeOffset(0), 0); QCOMPARE(tzp.daylightTimeOffset(0), 0); @@ -985,7 +985,7 @@ void tst_QTimeZone::utcTest() QCOMPARE(tz.comment(), QString("Qt Testing")); QCOMPARE(tz.country(), QLocale::Norway); QCOMPARE(tz.abbreviation(now), QString("QST")); - QCOMPARE(tz.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()), + QCOMPARE(tz.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QLocale()), QString("Qt Standard Time")); QCOMPARE(tz.offsetFromUtc(now), 123456); QCOMPARE(tz.standardTimeOffset(now), 123456);