From a418fd5cbbf2d121c93d27194c02eb36aa4ac97e Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 11 May 2020 10:26:50 +0200 Subject: [PATCH] Improve casting Make a handful of narrowing casts explicit Change-Id: I318e9778840f2437963377b6b97f269d569909dc Reviewed-by: Sona Kurazyan --- src/corelib/global/qfloat16.h | 10 +++++----- src/corelib/text/qbytearraymatcher.h | 2 +- src/corelib/text/qstringiterator_p.h | 2 +- src/corelib/tools/qtools_p.h | 2 +- src/corelib/tools/qversionnumber.h | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h index a25130958f..9ac811a3e6 100644 --- a/src/corelib/global/qfloat16.h +++ b/src/corelib/global/qfloat16.h @@ -72,7 +72,7 @@ class qfloat16 // To let our private constructor work, without other code seeing // ambiguity when constructing from int, double &c. quint16 b16; - constexpr inline explicit Wrap(int value) : b16(value) {} + constexpr inline explicit Wrap(int value) : b16(quint16(value)) {} }; public: constexpr inline qfloat16() noexcept : b16(0) {} @@ -172,8 +172,8 @@ inline qfloat16::qfloat16(float f) noexcept #else quint32 u; memcpy(&u, &f, sizeof(quint32)); - b16 = basetable[(u >> 23) & 0x1ff] - + ((u & 0x007fffff) >> shifttable[(u >> 23) & 0x1ff]); + b16 = quint16(basetable[(u >> 23) & 0x1ff] + + ((u & 0x007fffff) >> shifttable[(u >> 23) & 0x1ff])); #endif } QT_WARNING_POP @@ -268,8 +268,8 @@ QF16_MAKE_BOOL_OP(float) #undef QF16_MAKE_BOOL_OP_FP #define QF16_MAKE_BOOL_OP_INT(OP) \ - inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast(a) OP b; } \ - inline bool operator OP(int a, qfloat16 b) noexcept { return a OP static_cast(b); } + inline bool operator OP(qfloat16 a, int b) noexcept { return static_cast(a) OP static_cast(b); } \ + inline bool operator OP(int a, qfloat16 b) noexcept { return static_cast(a) OP static_cast(b); } QF16_MAKE_BOOL_OP_INT(>) QF16_MAKE_BOOL_OP_INT(<) QF16_MAKE_BOOL_OP_INT(>=) diff --git a/src/corelib/text/qbytearraymatcher.h b/src/corelib/text/qbytearraymatcher.h index 95de7416a0..c923bd15d5 100644 --- a/src/corelib/text/qbytearraymatcher.h +++ b/src/corelib/text/qbytearraymatcher.h @@ -101,7 +101,7 @@ private: static Q_DECL_RELAXED_CONSTEXPR Skiptable generate(const char *pattern, uint n) noexcept { const auto uchar_max = (std::numeric_limits::max)(); - uchar max = n > uchar_max ? uchar_max : n; + uchar max = n > uchar_max ? uchar_max : uchar(n); Skiptable table = { // this verbose initialization code aims to avoid some opaque error messages // even on powerful compilers such as GCC 5.3. Even though for GCC a loop diff --git a/src/corelib/text/qstringiterator_p.h b/src/corelib/text/qstringiterator_p.h index 6f9e82389e..0ee5eb0e53 100644 --- a/src/corelib/text/qstringiterator_p.h +++ b/src/corelib/text/qstringiterator_p.h @@ -90,7 +90,7 @@ public: inline int index() const { - return pos - i; + return int(pos - i); } inline void setPosition(QString::const_iterator position) diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h index d48318b474..7a1e2b064f 100644 --- a/src/corelib/tools/qtools_p.h +++ b/src/corelib/tools/qtools_p.h @@ -77,7 +77,7 @@ Q_DECL_CONSTEXPR inline int fromHex(uint c) noexcept Q_DECL_CONSTEXPR inline char toOct(uint value) noexcept { - return '0' + char(value & 0x7); + return char('0' + (value & 0x7)); } Q_DECL_CONSTEXPR inline int fromOct(uint c) noexcept diff --git a/src/corelib/tools/qversionnumber.h b/src/corelib/tools/qversionnumber.h index d30dcd47a3..f4e384c8dc 100644 --- a/src/corelib/tools/qversionnumber.h +++ b/src/corelib/tools/qversionnumber.h @@ -157,7 +157,7 @@ class QVersionNumber { return isUsingPointer() ? pointer_segments->size() : (inline_segments[InlineSegmentMarker] >> 1); } void setInlineSize(int len) - { inline_segments[InlineSegmentMarker] = 1 + 2 * len; } + { inline_segments[InlineSegmentMarker] = qint8(1 + 2 * len); } void resize(int len) {