From 190e7bd30a8df5bfa8e1aba0210f2b67a3b0120d Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sun, 5 Nov 2023 12:37:04 +0100 Subject: Issue #2342: Use [[nodiscard]] in /mfbt --- mfbt/AllocPolicy.h | 2 +- mfbt/AlreadyAddRefed.h | 2 +- mfbt/Assertions.h | 8 ++++---- mfbt/Compression.h | 6 +++--- mfbt/EndianUtils.h | 24 +++++++++++----------- mfbt/FloatingPoint.h | 2 +- mfbt/HashFunctions.h | 26 +++++++++++------------ mfbt/SegmentedVector.h | 2 +- mfbt/ThreadLocal.h | 2 +- mfbt/UniquePtr.h | 4 ++-- mfbt/Vector.h | 42 +++++++++++++++++++------------------- mfbt/tests/TestSegmentedVector.cpp | 2 +- 12 files changed, 61 insertions(+), 61 deletions(-) diff --git a/mfbt/AllocPolicy.h b/mfbt/AllocPolicy.h index add1a1a34a..56f7ea0d7f 100644 --- a/mfbt/AllocPolicy.h +++ b/mfbt/AllocPolicy.h @@ -121,7 +121,7 @@ public: { } - MOZ_MUST_USE bool checkSimulatedOOM() const + [[nodiscard]] bool checkSimulatedOOM() const { return true; } diff --git a/mfbt/AlreadyAddRefed.h b/mfbt/AlreadyAddRefed.h index 1478ce9e8a..6d356da438 100644 --- a/mfbt/AlreadyAddRefed.h +++ b/mfbt/AlreadyAddRefed.h @@ -110,7 +110,7 @@ struct MOZ_MUST_USE_TYPE MOZ_NON_AUTOABLE already_AddRefed aUnused << mutableAlreadyAddRefed->take(); } - MOZ_MUST_USE T* take() + [[nodiscard]] T* take() { T* rawPtr = mRawPtr; mRawPtr = nullptr; diff --git a/mfbt/Assertions.h b/mfbt/Assertions.h index df41795c12..09a1b8eaa6 100644 --- a/mfbt/Assertions.h +++ b/mfbt/Assertions.h @@ -603,25 +603,25 @@ struct AssertionConditionType # define MOZ_ALWAYS_TRUE(expr) \ do { \ if ((expr)) { \ - /* Silence MOZ_MUST_USE. */ \ + /* Silence [[nodiscard]]. */ \ } \ } while (0) # define MOZ_ALWAYS_FALSE(expr) \ do { \ if ((expr)) { \ - /* Silence MOZ_MUST_USE. */ \ + /* Silence [[nodiscard]]. */ \ } \ } while (0) # define MOZ_ALWAYS_OK(expr) \ do { \ if ((expr).isOk()) { \ - /* Silence MOZ_MUST_USE. */ \ + /* Silence [[nodiscard]]. */ \ } \ } while (0) # define MOZ_ALWAYS_ERR(expr) \ do { \ if ((expr).isErr()) { \ - /* Silence MOZ_MUST_USE. */ \ + /* Silence [[nodiscard]]. */ \ } \ } while (0) #endif diff --git a/mfbt/Compression.h b/mfbt/Compression.h index 37e4307c0d..cf207c6fa0 100644 --- a/mfbt/Compression.h +++ b/mfbt/Compression.h @@ -71,7 +71,7 @@ public: * @param aOutputSize is the output size, therefore the original size * @return true on success, false on failure */ - static MFBT_API MOZ_MUST_USE bool + [[nodiscard]] static MFBT_API bool decompress(const char* aSource, char* aDest, size_t aOutputSize); /** @@ -91,7 +91,7 @@ public: * buffer (necessarily <= aMaxOutputSize) * @return true on success, false on failure */ - static MFBT_API MOZ_MUST_USE bool + [[nodiscard]] static MFBT_API bool decompress(const char* aSource, size_t aInputSize, char* aDest, size_t aMaxOutputSize, size_t* aOutputSize); @@ -114,7 +114,7 @@ public: * buffer (necessarily <= aMaxOutputSize) * @return true on success, false on failure */ - static MFBT_API MOZ_MUST_USE bool + [[nodiscard]] static MFBT_API bool decompressPartial(const char* aSource, size_t aInputSize, char* aDest, size_t aMaxOutputSize, size_t* aOutputSize); diff --git a/mfbt/EndianUtils.h b/mfbt/EndianUtils.h index 719e048c12..1dce2bdc29 100644 --- a/mfbt/EndianUtils.h +++ b/mfbt/EndianUtils.h @@ -343,37 +343,37 @@ class Endian : private EndianUtils { protected: /** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE uint16_t readUint16(const void* aPtr) + [[nodiscard]] static uint16_t readUint16(const void* aPtr) { return read(aPtr); } /** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE uint32_t readUint32(const void* aPtr) + [[nodiscard]] static uint32_t readUint32(const void* aPtr) { return read(aPtr); } /** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE uint64_t readUint64(const void* aPtr) + [[nodiscard]] static uint64_t readUint64(const void* aPtr) { return read(aPtr); } /** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE int16_t readInt16(const void* aPtr) + [[nodiscard]] static int16_t readInt16(const void* aPtr) { return read(aPtr); } /** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE int32_t readInt32(const void* aPtr) + [[nodiscard]] static int32_t readInt32(const void* aPtr) { return read(aPtr); } /** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE int64_t readInt64(const void* aPtr) + [[nodiscard]] static int64_t readInt64(const void* aPtr) { return read(aPtr); } @@ -422,7 +422,7 @@ protected: * format for transmission. */ template - MOZ_MUST_USE static T swapToLittleEndian(T aValue) + [[nodiscard]] static T swapToLittleEndian(T aValue) { return maybeSwap(aValue); } @@ -452,7 +452,7 @@ protected: * Converts a value of type T to big-endian format. */ template - MOZ_MUST_USE static T swapToBigEndian(T aValue) + [[nodiscard]] static T swapToBigEndian(T aValue) { return maybeSwap(aValue); } @@ -484,7 +484,7 @@ protected: */ template - MOZ_MUST_USE static T swapToNetworkOrder(T aValue) + [[nodiscard]] static T swapToNetworkOrder(T aValue) { return swapToBigEndian(aValue); } @@ -507,7 +507,7 @@ protected: * Converts a value of type T from little-endian format. */ template - MOZ_MUST_USE static T swapFromLittleEndian(T aValue) + [[nodiscard]] static T swapFromLittleEndian(T aValue) { return maybeSwap(aValue); } @@ -537,7 +537,7 @@ protected: * Converts a value of type T from big-endian format. */ template - MOZ_MUST_USE static T swapFromBigEndian(T aValue) + [[nodiscard]] static T swapFromBigEndian(T aValue) { return maybeSwap(aValue); } @@ -568,7 +568,7 @@ protected: * in network code. */ template - MOZ_MUST_USE static T swapFromNetworkOrder(T aValue) + [[nodiscard]] static T swapFromNetworkOrder(T aValue) { return swapFromBigEndian(aValue); } diff --git a/mfbt/FloatingPoint.h b/mfbt/FloatingPoint.h index a2846ce298..2ee1d1299b 100644 --- a/mfbt/FloatingPoint.h +++ b/mfbt/FloatingPoint.h @@ -523,7 +523,7 @@ FuzzyEqualsMultiplicative(T aValue1, T aValue2, * * This function isn't inlined to avoid buggy optimizations by MSVC. */ -MOZ_MUST_USE +[[nodiscard]] extern MFBT_API bool IsFloat32Representable(double aFloat32); diff --git a/mfbt/HashFunctions.h b/mfbt/HashFunctions.h index d287081174..8a3bacd966 100644 --- a/mfbt/HashFunctions.h +++ b/mfbt/HashFunctions.h @@ -160,7 +160,7 @@ AddUintptrToHash<8>(uint32_t aHash, uintptr_t aValue) * convert to uint32_t, data pointers, and function pointers. */ template -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t AddToHash(uint32_t aHash, A aA) { /* @@ -171,7 +171,7 @@ AddToHash(uint32_t aHash, A aA) } template -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t AddToHash(uint32_t aHash, A* aA) { /* @@ -185,14 +185,14 @@ AddToHash(uint32_t aHash, A* aA) } template<> -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t AddToHash(uint32_t aHash, uintptr_t aA) { return detail::AddUintptrToHash(aHash, aA); } template -MOZ_MUST_USE uint32_t +[[nodiscard]] uint32_t AddToHash(uint32_t aHash, A aArg, Args... aArgs) { return AddToHash(AddToHash(aHash, aArg), aArgs...); @@ -206,7 +206,7 @@ AddToHash(uint32_t aHash, A aArg, Args... aArgs) * that x has already been hashed. */ template -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashGeneric(Args... aArgs) { return AddToHash(0, aArgs...); @@ -244,32 +244,32 @@ HashKnownLength(const T* aStr, size_t aLength) * If you have the string's length, you might as well call the overload which * includes the length. It may be marginally faster. */ -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const char* aStr) { return detail::HashUntilZero(reinterpret_cast(aStr)); } -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const char* aStr, size_t aLength) { return detail::HashKnownLength(reinterpret_cast(aStr), aLength); } -MOZ_MUST_USE +[[nodiscard]] inline uint32_t HashString(const unsigned char* aStr, size_t aLength) { return detail::HashKnownLength(aStr, aLength); } -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const char16_t* aStr) { return detail::HashUntilZero(aStr); } -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const char16_t* aStr, size_t aLength) { return detail::HashKnownLength(aStr, aLength); @@ -280,13 +280,13 @@ HashString(const char16_t* aStr, size_t aLength) * the same width! */ #ifdef WIN32 -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const wchar_t* aStr) { return detail::HashUntilZero(aStr); } -MOZ_MUST_USE inline uint32_t +[[nodiscard]] inline uint32_t HashString(const wchar_t* aStr, size_t aLength) { return detail::HashKnownLength(aStr, aLength); @@ -299,7 +299,7 @@ HashString(const wchar_t* aStr, size_t aLength) * This hash walks word-by-word, rather than byte-by-byte, so you won't get the * same result out of HashBytes as you would out of HashString. */ -MOZ_MUST_USE extern MFBT_API uint32_t +[[nodiscard]] extern MFBT_API uint32_t HashBytes(const void* bytes, size_t aLength); /** diff --git a/mfbt/SegmentedVector.h b/mfbt/SegmentedVector.h index 6e4dc2a452..54590a934d 100644 --- a/mfbt/SegmentedVector.h +++ b/mfbt/SegmentedVector.h @@ -158,7 +158,7 @@ public: // Returns false if the allocation failed. (If you are using an infallible // allocation policy, use InfallibleAppend() instead.) template - MOZ_MUST_USE bool Append(U&& aU) + [[nodiscard]] bool Append(U&& aU) { Segment* last = mSegments.getLast(); if (!last || last->Length() == kSegmentCapacity) { diff --git a/mfbt/ThreadLocal.h b/mfbt/ThreadLocal.h index 7acfa46548..cf8a97cba2 100644 --- a/mfbt/ThreadLocal.h +++ b/mfbt/ThreadLocal.h @@ -113,7 +113,7 @@ public: {} #endif - MOZ_MUST_USE inline bool init(); + [[nodiscard]] inline bool init(); inline T get() const; diff --git a/mfbt/UniquePtr.h b/mfbt/UniquePtr.h index 97fb8d27ae..035adc2a52 100644 --- a/mfbt/UniquePtr.h +++ b/mfbt/UniquePtr.h @@ -327,7 +327,7 @@ public: DeleterType& get_deleter() { return del(); } const DeleterType& get_deleter() const { return del(); } - MOZ_MUST_USE Pointer release() + [[nodiscard]] Pointer release() { Pointer p = ptr(); ptr() = nullptr; @@ -462,7 +462,7 @@ public: DeleterType& get_deleter() { return mTuple.second(); } const DeleterType& get_deleter() const { return mTuple.second(); } - MOZ_MUST_USE Pointer release() + [[nodiscard]] Pointer release() { Pointer p = mTuple.first(); mTuple.first() = nullptr; diff --git a/mfbt/Vector.h b/mfbt/Vector.h index 6d5c769e9d..b75a5d8a04 100644 --- a/mfbt/Vector.h +++ b/mfbt/Vector.h @@ -129,7 +129,7 @@ struct VectorImpl * aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will * not overflow. */ - static inline MOZ_MUST_USE bool + [[nodiscard]] static inline bool growTo(Vector& aV, size_t aNewCap) { MOZ_ASSERT(!aV.usingInlineStorage()); @@ -221,7 +221,7 @@ struct VectorImpl } } - static inline MOZ_MUST_USE bool + [[nodiscard]] static inline bool growTo(Vector& aV, size_t aNewCap) { MOZ_ASSERT(!aV.usingInlineStorage()); @@ -290,9 +290,9 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy friend struct detail::VectorTesting; - MOZ_MUST_USE bool growStorageBy(size_t aIncr); - MOZ_MUST_USE bool convertToHeapStorage(size_t aNewCap); - MOZ_MUST_USE bool maybeCheckSimulatedOOM(size_t aRequestedSize); + [[nodiscard]] bool growStorageBy(size_t aIncr); + [[nodiscard]] bool convertToHeapStorage(size_t aNewCap); + [[nodiscard]] bool maybeCheckSimulatedOOM(size_t aRequestedSize); /* magic constants */ @@ -618,7 +618,7 @@ public: * Given that the vector is empty, grow the internal capacity to |aRequest|, * keeping the length 0. */ - MOZ_MUST_USE bool initCapacity(size_t aRequest); + [[nodiscard]] bool initCapacity(size_t aRequest); /** * Given that the vector is empty, grow the internal capacity and length to @@ -627,7 +627,7 @@ public: * rounding that happens in resize and overhead of initialization for elements * that are about to be overwritten. */ - MOZ_MUST_USE bool initLengthUninitialized(size_t aRequest); + [[nodiscard]] bool initLengthUninitialized(size_t aRequest); /** * If reserve(aRequest) succeeds and |aRequest >= length()|, then appending @@ -637,7 +637,7 @@ public: * A request to reserve an amount less than the current length does not affect * reserved space. */ - MOZ_MUST_USE bool reserve(size_t aRequest); + [[nodiscard]] bool reserve(size_t aRequest); /** * Destroy elements in the range [end() - aIncr, end()). Does not deallocate @@ -652,18 +652,18 @@ public: void shrinkTo(size_t aNewLength); /** Grow the vector by aIncr elements. */ - MOZ_MUST_USE bool growBy(size_t aIncr); + [[nodiscard]] bool growBy(size_t aIncr); /** Call shrinkBy or growBy based on whether newSize > length(). */ - MOZ_MUST_USE bool resize(size_t aNewLength); + [[nodiscard]] bool resize(size_t aNewLength); /** * Increase the length of the vector, but don't initialize the new elements * -- leave them as uninitialized memory. */ - MOZ_MUST_USE bool growByUninitialized(size_t aIncr); + [[nodiscard]] bool growByUninitialized(size_t aIncr); void infallibleGrowByUninitialized(size_t aIncr); - MOZ_MUST_USE bool resizeUninitialized(size_t aNewLength); + [[nodiscard]] bool resizeUninitialized(size_t aNewLength); /** Shorthand for shrinkBy(length()). */ void clear(); @@ -693,13 +693,13 @@ public: * vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are * not amused.") */ - template MOZ_MUST_USE bool append(U&& aU); + template [[nodiscard]] bool append(U&& aU); /** * Construct a T in-place as a new entry at the end of this vector. */ template - MOZ_MUST_USE bool emplaceBack(Args&&... aArgs) + [[nodiscard]] bool emplaceBack(Args&&... aArgs) { if (!growByUninitialized(1)) return false; @@ -708,10 +708,10 @@ public: } template - MOZ_MUST_USE bool appendAll(const Vector& aU); - MOZ_MUST_USE bool appendN(const T& aT, size_t aN); - template MOZ_MUST_USE bool append(const U* aBegin, const U* aEnd); - template MOZ_MUST_USE bool append(const U* aBegin, size_t aLength); + [[nodiscard]] bool appendAll(const Vector& aU); + [[nodiscard]] bool appendN(const T& aT, size_t aN); + template [[nodiscard]] bool append(const U* aBegin, const U* aEnd); + template [[nodiscard]] bool append(const U* aBegin, size_t aLength); /* * Guaranteed-infallible append operations for use upon vectors whose @@ -755,7 +755,7 @@ public: * * N.B. Although a T*, only the range [0, length()) is constructed. */ - MOZ_MUST_USE T* extractRawBuffer(); + [[nodiscard]] T* extractRawBuffer(); /** * If elements are stored in-place, allocate a new buffer, move this vector's @@ -773,7 +773,7 @@ public: * If any of these elements are uninitialized (as growByUninitialized * enables), behavior is undefined. */ - MOZ_MUST_USE T* extractOrCopyRawBuffer(); + [[nodiscard]] T* extractOrCopyRawBuffer(); /** * Transfer ownership of an array of objects into the vector. The caller @@ -801,7 +801,7 @@ public: * This is inherently a linear-time operation. Be careful! */ template - MOZ_MUST_USE T* insert(T* aP, U&& aVal); + [[nodiscard]] T* insert(T* aP, U&& aVal); /** * Removes the element |aT|, which must fall in the bounds [begin, end), diff --git a/mfbt/tests/TestSegmentedVector.cpp b/mfbt/tests/TestSegmentedVector.cpp index 31f9f47432..0e45c64bcf 100644 --- a/mfbt/tests/TestSegmentedVector.cpp +++ b/mfbt/tests/TestSegmentedVector.cpp @@ -34,7 +34,7 @@ public: }; // We want to test Append(), which is fallible and marked with -// MOZ_MUST_USE. But we're using an infallible alloc policy, and so +// [[nodiscard]]. But we're using an infallible alloc policy, and so // don't really need to check the result. Casting to |void| works with clang // but not GCC, so we instead use this dummy variable which works with both // compilers. -- cgit v1.2.3