diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 1ecb19f135..459e00887d 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1173,8 +1173,8 @@ QByteArray &QByteArray::operator=(const char *str) } else { const qsizetype len = qsizetype(strlen(str)); const auto capacityAtEnd = d->allocatedCapacity() - d.freeSpaceAtBegin(); - if (d->needsDetach() || size_t(len) > capacityAtEnd - || (len < size() && size_t(len) < (capacityAtEnd >> 1))) + if (d->needsDetach() || len > capacityAtEnd + || (len < size() && len < (capacityAtEnd >> 1))) reallocData(len, d->detachFlags()); memcpy(d.data(), str, len + 1); // include null terminator d.size = len; diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index b0834ad762..9626f4eeff 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -1186,7 +1186,7 @@ protected: { Q_ASSERT(this->isMutable() || required == 0); Q_ASSERT(!this->isShared() || required == 0); - Q_ASSERT(required <= this->constAllocatedCapacity() - this->size); + Q_ASSERT(required <= size_t(this->constAllocatedCapacity() - this->size)); using MoveOps = std::conditional_t::isRelocatable, RelocatableMoveOps, @@ -1279,7 +1279,7 @@ protected: // container. This is insert-specific helper function qsizetype sizeToInsertAtBegin(const T *const where, qsizetype maxSize) { - Q_ASSERT(size_t(maxSize) <= this->allocatedCapacity() - this->size); + Q_ASSERT(maxSize <= this->allocatedCapacity() - this->size); Q_ASSERT(where >= this->begin() && where <= this->end()); // in range const auto freeAtBegin = this->freeSpaceAtBegin(); @@ -1349,7 +1349,7 @@ public: Q_ASSERT(this->isMutable()); Q_ASSERT(!this->isShared()); Q_ASSERT(newSize > size_t(this->size)); - Q_ASSERT(newSize <= this->allocatedCapacity()); + Q_ASSERT(newSize <= size_t(this->allocatedCapacity())); // Since this is mostly an initialization function, do not follow append // logic of space arrangement. Instead, only prepare as much free space @@ -1366,7 +1366,7 @@ public: Q_ASSERT(this->isMutable() || b == e); Q_ASSERT(!this->isShared() || b == e); Q_ASSERT(b <= e); - Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size); + Q_ASSERT((e - b) <= this->allocatedCapacity() - this->size); if (b == e) // short-cut and handling the case b and e == nullptr return; @@ -1382,7 +1382,7 @@ public: Q_ASSERT(this->isMutable() || b == e); Q_ASSERT(!this->isShared() || b == e); const qsizetype distance = std::distance(b, e); - Q_ASSERT(distance >= 0 && size_t(distance) <= this->allocatedCapacity() - this->size); + Q_ASSERT(distance >= 0 && distance <= this->allocatedCapacity() - this->size); prepareSpaceForAppend(b, e, distance); // ### perf. loss @@ -1398,7 +1398,7 @@ public: Q_ASSERT(this->isMutable() || b == e); Q_ASSERT(!this->isShared() || b == e); Q_ASSERT(b <= e); - Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size); + Q_ASSERT((e - b) <= this->allocatedCapacity() - this->size); if (b == e) // short-cut and handling the case b and e == nullptr return; @@ -1409,7 +1409,7 @@ public: void copyAppend(size_t n, parameter_type t) { Q_ASSERT(!this->isShared() || n == 0); - Q_ASSERT(this->allocatedCapacity() - size_t(this->size) >= n); + Q_ASSERT(size_t(this->allocatedCapacity() - this->size) >= n); // Preserve the value, because it might be a reference to some part of the moved chunk T tmp(t); @@ -1424,7 +1424,7 @@ public: Q_ASSERT(where >= this->begin() && where <= this->end()); Q_ASSERT(b <= e); Q_ASSERT(e <= where || b > this->end() || where == this->end()); // No overlap or append - Q_ASSERT(size_t(e - b) <= this->allocatedCapacity() - this->size); + Q_ASSERT((e - b) <= this->allocatedCapacity() - this->size); if (b == e) // short-cut and handling the case b and e == nullptr return; @@ -1451,7 +1451,7 @@ public: { Q_ASSERT(!this->isShared() || (n == 0 && where == this->end())); Q_ASSERT(where >= this->begin() && where <= this->end()); - Q_ASSERT(this->allocatedCapacity() - size_t(this->size) >= n); + Q_ASSERT(size_t(this->allocatedCapacity() - this->size) >= n); if (this->size > 0 && where == this->begin()) { // prepend case - special space arrangement // Preserve the value, because it might be a reference to some part of the moved chunk diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 7f122b156c..ce5710fa07 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -173,15 +173,15 @@ public: } // forwards from QArrayData - size_t allocatedCapacity() noexcept { return d ? d->allocatedCapacity() : 0; } - size_t constAllocatedCapacity() const noexcept { return d ? d->constAllocatedCapacity() : 0; } + qsizetype allocatedCapacity() noexcept { return d ? d->allocatedCapacity() : 0; } + qsizetype constAllocatedCapacity() const noexcept { return d ? d->constAllocatedCapacity() : 0; } void ref() noexcept { if (d) d->ref(); } bool deref() noexcept { return !d || d->deref(); } bool isMutable() const noexcept { return d; } bool isShared() const noexcept { return !d || d->isShared(); } bool isSharedWith(const QArrayDataPointer &other) const noexcept { return d && d == other.d; } bool needsDetach() const noexcept { return !d || d->needsDetach(); } - size_t detachCapacity(size_t newSize) const noexcept { return d ? d->detachCapacity(newSize) : newSize; } + qsizetype detachCapacity(qsizetype newSize) const noexcept { return d ? d->detachCapacity(newSize) : newSize; } const typename Data::ArrayOptions flags() const noexcept { return d ? typename Data::ArrayOption(d->flags) : Data::DefaultAllocationFlags; } void setFlag(typename Data::ArrayOptions f) noexcept { Q_ASSERT(d); d->flags |= f; } void clearFlag(typename Data::ArrayOptions f) noexcept { Q_ASSERT(d); d->flags &= ~f; } diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 80a8d1eec9..185bcc5086 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -545,7 +545,7 @@ inline void QList::remove(qsizetype i, qsizetype n) if (n == 0) return; - const size_t newSize = size() - n; + const auto newSize = size() - n; if (d->needsDetach() || ((d->flags() & Data::CapacityReserved) == 0 && newSize < d->allocatedCapacity()/2)) { @@ -581,8 +581,8 @@ inline void QList::append(const_iterator i1, const_iterator i2) { if (i1 == i2) return; - const size_t distance = std::distance(i1, i2); - const size_t newSize = size() + distance; + const auto distance = std::distance(i1, i2); + const auto newSize = size() + distance; const bool shouldGrow = d->shouldGrowBeforeInsert(d.end(), qsizetype(distance)); if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) { DataPointer detached(DataPointer::allocateGrow(d, newSize, @@ -604,7 +604,7 @@ inline void QList::append(QList &&other) if (other.d->needsDetach() || !std::is_nothrow_move_constructible_v) return append(other); - const size_t newSize = size() + other.size(); + const auto newSize = size() + other.size(); const bool shouldGrow = d->shouldGrowBeforeInsert(d.end(), other.size()); if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) { DataPointer detached(DataPointer::allocateGrow(d, newSize, @@ -633,7 +633,7 @@ QList::insert(qsizetype i, qsizetype n, parameter_type t) // we don't have a quick exit for n == 0 // it's not worth wasting CPU cycles for that - const size_t newSize = size() + n; + const auto newSize = size() + n; const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + i, n); if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) { typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; @@ -666,7 +666,7 @@ QList::emplace(qsizetype i, Args&&... args) Q_ASSERT_X(i >= 0 && i <= d->size, "QList::insert", "index out of range"); const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin() + i, 1); - const size_t newSize = size() + 1; + const auto newSize = size() + 1; if (d->needsDetach() || newSize > d->allocatedCapacity() || shouldGrow) { typename Data::ArrayOptions flags = d->detachFlags() | Data::GrowsForward; if (d.size != 0 && i <= d.size / 4) diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 288bc6e120..c9b5538ad5 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -1179,17 +1179,17 @@ void tst_QArrayData::arrayOpsExtra() auto [intData, strData, objData] = setupDataPointers(inputSize); QVERIFY(intData.size == 0); QVERIFY(intData.d_ptr() != nullptr); - QVERIFY(intData.constAllocatedCapacity() >= inputSize); + QVERIFY(size_t(intData.constAllocatedCapacity()) >= inputSize); QVERIFY(intData.data() != nullptr); QVERIFY(strData.size == 0); QVERIFY(strData.d_ptr() != nullptr); - QVERIFY(strData.constAllocatedCapacity() >= inputSize); + QVERIFY(size_t(strData.constAllocatedCapacity()) >= inputSize); QVERIFY(strData.data() != nullptr); QVERIFY(objData.size == 0); QVERIFY(objData.d_ptr() != nullptr); - QVERIFY(objData.constAllocatedCapacity() >= inputSize); + QVERIFY(size_t(objData.constAllocatedCapacity()) >= inputSize); QVERIFY(objData.data() != nullptr); } @@ -1237,9 +1237,9 @@ void tst_QArrayData::arrayOpsExtra() RUN_TEST_FUNC(testCopyAppend, intData, intVec.begin(), intVec.end()); RUN_TEST_FUNC(testCopyAppend, strData, strVec.begin(), strVec.end()); RUN_TEST_FUNC(testCopyAppend, objData, objVec.begin(), objVec.end()); - QCOMPARE(size_t(intData.size), intData.constAllocatedCapacity()); - QCOMPARE(size_t(strData.size), strData.constAllocatedCapacity()); - QCOMPARE(size_t(objData.size), objData.constAllocatedCapacity()); + QCOMPARE(intData.size, intData.constAllocatedCapacity()); + QCOMPARE(strData.size, strData.constAllocatedCapacity()); + QCOMPARE(objData.size, objData.constAllocatedCapacity()); } // copyAppend (iterator version) - special case of copying from self iterators @@ -1337,9 +1337,9 @@ void tst_QArrayData::arrayOpsExtra() RUN_TEST_FUNC(testCopyAppend, intData, intDataFreeSpace, int(-1)); RUN_TEST_FUNC(testCopyAppend, strData, strDataFreeSpace, QLatin1String("foo")); RUN_TEST_FUNC(testCopyAppend, objData, objDataFreeSpace, CountedObject()); - QCOMPARE(size_t(intData.size), intData.constAllocatedCapacity()); - QCOMPARE(size_t(strData.size), strData.constAllocatedCapacity()); - QCOMPARE(size_t(objData.size), objData.constAllocatedCapacity()); + QCOMPARE(intData.size, intData.constAllocatedCapacity()); + QCOMPARE(strData.size, strData.constAllocatedCapacity()); + QCOMPARE(objData.size, objData.constAllocatedCapacity()); } // copyAppend (value version) - special case of copying self value @@ -1431,9 +1431,9 @@ void tst_QArrayData::arrayOpsExtra() std::vector(strDataFreeSpace, QLatin1String("barbaz"))); RUN_TEST_FUNC(testMoveAppend, objData, std::vector(objDataFreeSpace, CountedObject())); - QCOMPARE(size_t(intData.size), intData.constAllocatedCapacity()); - QCOMPARE(size_t(strData.size), strData.constAllocatedCapacity()); - QCOMPARE(size_t(objData.size), objData.constAllocatedCapacity()); + QCOMPARE(intData.size, intData.constAllocatedCapacity()); + QCOMPARE(strData.size, strData.constAllocatedCapacity()); + QCOMPARE(objData.size, objData.constAllocatedCapacity()); } // moveAppend - special case of moving from self (this is legal yet rather useless) @@ -2104,7 +2104,7 @@ void tst_QArrayData::dataPointerAllocate() const auto freeAtEnd = newDataPointer.freeSpaceAtEnd(); QVERIFY(newAlloc > oldDataPointer.constAllocatedCapacity()); - QCOMPARE(size_t(freeAtBegin + freeAtEnd), newAlloc); + QCOMPARE(freeAtBegin + freeAtEnd, newAlloc); // when not detached, the behavior is the same as of ::realloc if (allocationOptions & (QArrayData::GrowsForward | QArrayData::GrowsBackwards)) QCOMPARE(freeAtBegin, oldDataPointer.freeSpaceAtBegin()); @@ -2136,9 +2136,9 @@ void tst_QArrayData::dataPointerAllocate() const auto freeAtEnd = newDataPointer.freeSpaceAtEnd(); QVERIFY(newAlloc > oldDataPointer.constAllocatedCapacity()); - QCOMPARE(size_t(freeAtBegin + freeAtEnd), newAlloc); + QCOMPARE(freeAtBegin + freeAtEnd, newAlloc); if (allocationOptions & QArrayData::GrowsBackwards) { - QCOMPARE(size_t(freeAtBegin), (newAlloc - newSize) / 2); + QCOMPARE(freeAtBegin, (newAlloc - newSize) / 2); } else { QCOMPARE(freeAtBegin, 0); }