From 7bc752e42b670e32ff48ae5abbe0f819241a141c Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Wed, 17 Mar 2021 03:01:21 +0000 Subject: [PATCH] Bug 1571631 - Replace MOZ_MUST_USE with [[nodiscard]] in mfbt. r=sg Also move MOZ_MUST_USE before function declarations' specifiers and return type. While clang and gcc's __attribute__((warn_unused_result)) can appear before, between, or after function specifiers and return types, the [[nodiscard]] attribute must precede the function specifiers. Depends on D108344 Differential Revision: https://phabricator.services.mozilla.com/D108345 --- mfbt/AllocPolicy.h | 4 +-- mfbt/AlreadyAddRefed.h | 2 +- mfbt/Assertions.h | 2 +- mfbt/BufferList.h | 6 ++-- mfbt/Compression.h | 18 +++++------ mfbt/EndianUtils.h | 28 ++++++++--------- mfbt/FloatingPoint.h | 3 +- mfbt/HashFunctions.h | 40 ++++++++++++------------- mfbt/HashTable.h | 41 ++++++++++++------------- mfbt/PairHash.h | 4 +-- mfbt/SPSCQueue.h | 14 ++++----- mfbt/ScopeExit.h | 2 +- mfbt/SegmentedVector.h | 2 +- mfbt/ThreadLocal.h | 2 +- mfbt/UniquePtr.h | 4 +-- mfbt/Vector.h | 48 ++++++++++++++++-------------- mfbt/tests/TestSegmentedVector.cpp | 2 +- 17 files changed, 111 insertions(+), 111 deletions(-) diff --git a/mfbt/AllocPolicy.h b/mfbt/AllocPolicy.h index 7bf1428b9846..e5c62bcd640c 100644 --- a/mfbt/AllocPolicy.h +++ b/mfbt/AllocPolicy.h @@ -118,7 +118,7 @@ class MallocAllocPolicy { void reportAllocOverflow() const {} - MOZ_MUST_USE bool checkSimulatedOOM() const { return true; } + [[nodiscard]] bool checkSimulatedOOM() const { return true; } }; /* @@ -167,7 +167,7 @@ class NeverAllocPolicy { void reportAllocOverflow() const {} - MOZ_MUST_USE bool checkSimulatedOOM() const { return true; } + [[nodiscard]] bool checkSimulatedOOM() const { return true; } }; } // namespace mozilla diff --git a/mfbt/AlreadyAddRefed.h b/mfbt/AlreadyAddRefed.h index d03129eee94a..d75263b49f68 100644 --- a/mfbt/AlreadyAddRefed.h +++ b/mfbt/AlreadyAddRefed.h @@ -142,7 +142,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; return rawPtr; diff --git a/mfbt/Assertions.h b/mfbt/Assertions.h index d4aef43fe9e5..95cf926c7ebc 100644 --- a/mfbt/Assertions.h +++ b/mfbt/Assertions.h @@ -594,7 +594,7 @@ struct AssertionConditionType { #define MOZ_ALWAYS_TRUE(expr) \ do { \ if (MOZ_LIKELY(expr)) { \ - /* Silence MOZ_MUST_USE. */ \ + /* Silence [[nodiscard]]. */ \ } else { \ MOZ_DIAGNOSTIC_ASSERT(false, #expr); \ } \ diff --git a/mfbt/BufferList.h b/mfbt/BufferList.h index c12a3067a1df..06662c70fc2a 100644 --- a/mfbt/BufferList.h +++ b/mfbt/BufferList.h @@ -306,7 +306,7 @@ class BufferList : private AllocPolicy { // Copies aSize bytes from aData into the BufferList. The storage for these // bytes may be split across multiple buffers. Size() is increased by aSize. - inline MOZ_MUST_USE bool WriteBytes(const char* aData, size_t aSize); + [[nodiscard]] inline bool WriteBytes(const char* aData, size_t aSize); // Allocates a buffer of at most |aMaxBytes| bytes and, if successful, returns // that buffer, and places its size in |aSize|. If unsuccessful, returns null @@ -398,8 +398,8 @@ class BufferList : private AllocPolicy { }; template -MOZ_MUST_USE bool BufferList::WriteBytes(const char* aData, - size_t aSize) { +[[nodiscard]] bool BufferList::WriteBytes(const char* aData, + size_t aSize) { MOZ_RELEASE_ASSERT(mOwning); MOZ_RELEASE_ASSERT(mStandardCapacity); diff --git a/mfbt/Compression.h b/mfbt/Compression.h index 7c28cc366429..d9f787c0b46a 100644 --- a/mfbt/Compression.h +++ b/mfbt/Compression.h @@ -81,10 +81,10 @@ class LZ4 { * buffer (necessarily <= aMaxOutputSize) * @return true on success, false on failure */ - static MFBT_API MOZ_MUST_USE bool decompress(const char* aSource, - size_t aInputSize, char* aDest, - size_t aMaxOutputSize, - size_t* aOutputSize); + [[nodiscard]] static MFBT_API bool decompress(const char* aSource, + size_t aInputSize, char* aDest, + size_t aMaxOutputSize, + size_t* aOutputSize); /** * If the source stream is malformed, the function will stop decoding @@ -105,11 +105,11 @@ class LZ4 { * buffer (necessarily <= aMaxOutputSize) * @return true on success, false on failure */ - static MFBT_API MOZ_MUST_USE bool decompressPartial(const char* aSource, - size_t aInputSize, - char* aDest, - size_t aMaxOutputSize, - size_t* aOutputSize); + [[nodiscard]] static MFBT_API bool decompressPartial(const char* aSource, + size_t aInputSize, + char* aDest, + size_t aMaxOutputSize, + size_t* aOutputSize); /* * Provides the maximum size that LZ4 may output in a "worst case" diff --git a/mfbt/EndianUtils.h b/mfbt/EndianUtils.h index 8e76f3b8a79e..b6f3e2c315d0 100644 --- a/mfbt/EndianUtils.h +++ b/mfbt/EndianUtils.h @@ -280,42 +280,42 @@ template 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 a uintptr_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE uintptr_t readUintptr(const void* aPtr) { + [[nodiscard]] static uintptr_t readUintptr(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); } /** Read an intptr_t in ThisEndian endianness from |aPtr| and return it. */ - static MOZ_MUST_USE intptr_t readIntptr(const void* aPtr) { + [[nodiscard]] static intptr_t readIntptr(const void* aPtr) { return read(aPtr); } @@ -353,7 +353,7 @@ class Endian : private EndianUtils { * format for transmission. */ template - MOZ_MUST_USE static T swapToLittleEndian(T aValue) { + [[nodiscard]] static T swapToLittleEndian(T aValue) { return maybeSwap(aValue); } @@ -382,7 +382,7 @@ class Endian : private EndianUtils { * 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); } @@ -413,7 +413,7 @@ class Endian : private EndianUtils { */ template - MOZ_MUST_USE static T swapToNetworkOrder(T aValue) { + [[nodiscard]] static T swapToNetworkOrder(T aValue) { return swapToBigEndian(aValue); } @@ -432,7 +432,7 @@ class Endian : private EndianUtils { * 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); } @@ -461,7 +461,7 @@ class Endian : private EndianUtils { * 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); } @@ -491,7 +491,7 @@ class Endian : private EndianUtils { * 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 8d766df96577..09e9d88476ca 100644 --- a/mfbt/FloatingPoint.h +++ b/mfbt/FloatingPoint.h @@ -633,8 +633,7 @@ static MOZ_ALWAYS_INLINE bool FuzzyEqualsMultiplicative( * representable (even though the bit patterns of double precision NaNs can't * all be exactly represented in single precision). */ -MOZ_MUST_USE -extern MFBT_API bool IsFloat32Representable(double aValue); +[[nodiscard]] extern MFBT_API bool IsFloat32Representable(double aValue); } /* namespace mozilla */ diff --git a/mfbt/HashFunctions.h b/mfbt/HashFunctions.h index b08f4d56d9ef..060a539c9f16 100644 --- a/mfbt/HashFunctions.h +++ b/mfbt/HashFunctions.h @@ -181,7 +181,7 @@ inline HashNumber AddUintptrToHash<8>(HashNumber aHash, uintptr_t aValue) { template , bool TypeIsNotEnum = !std::is_enum_v, std::enable_if_t = 0> -MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, T aA) { +[[nodiscard]] inline HashNumber AddToHash(HashNumber aHash, T aA) { /* * Try to convert |A| to uint32_t implicitly. If this works, great. If not, * we'll error out. @@ -190,7 +190,7 @@ MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, T aA) { } template -MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, A* aA) { +[[nodiscard]] inline HashNumber AddToHash(HashNumber aHash, A* aA) { /* * You might think this function should just take a void*. But then we'd only * catch data pointers and couldn't handle function pointers. @@ -206,12 +206,12 @@ MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, A* aA) { // first implicitly converted to 32 bits and then passed to AddUintptrToHash() // to be hashed. template , int> = 0> -MOZ_MUST_USE constexpr HashNumber AddToHash(HashNumber aHash, T aA) { +[[nodiscard]] constexpr HashNumber AddToHash(HashNumber aHash, T aA) { return detail::AddUintptrToHash(aHash, aA); } template , int> = 0> -MOZ_MUST_USE constexpr HashNumber AddToHash(HashNumber aHash, T aA) { +[[nodiscard]] constexpr HashNumber AddToHash(HashNumber aHash, T aA) { // Hash using AddUintptrToHash with the underlying type of the enum type using UnderlyingType = typename std::underlying_type::type; return detail::AddUintptrToHash( @@ -219,7 +219,7 @@ MOZ_MUST_USE constexpr HashNumber AddToHash(HashNumber aHash, T aA) { } template -MOZ_MUST_USE HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) { +[[nodiscard]] HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) { return AddToHash(AddToHash(aHash, aArg), aArgs...); } @@ -231,7 +231,7 @@ MOZ_MUST_USE HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) { * that x has already been hashed. */ template -MOZ_MUST_USE inline HashNumber HashGeneric(Args... aArgs) { +[[nodiscard]] inline HashNumber HashGeneric(Args... aArgs) { return AddToHash(0, aArgs...); } @@ -248,7 +248,7 @@ MOZ_MUST_USE inline HashNumber HashGeneric(Args... aArgs) { * marginally faster. */ template -MOZ_MUST_USE constexpr HashNumber HashStringUntilZero(Iterator aIter) { +[[nodiscard]] constexpr HashNumber HashStringUntilZero(Iterator aIter) { HashNumber hash = 0; for (; auto c = *aIter; ++aIter) { hash = AddToHash(hash, c); @@ -260,8 +260,8 @@ MOZ_MUST_USE constexpr HashNumber HashStringUntilZero(Iterator aIter) { * Hash successive |aIter[i]| up to |i == aLength|. */ template -MOZ_MUST_USE constexpr HashNumber HashStringKnownLength(Iterator aIter, - size_t aLength) { +[[nodiscard]] constexpr HashNumber HashStringKnownLength(Iterator aIter, + size_t aLength) { HashNumber hash = 0; for (size_t i = 0; i < aLength; i++) { hash = AddToHash(hash, aIter[i]); @@ -275,30 +275,30 @@ MOZ_MUST_USE constexpr HashNumber HashStringKnownLength(Iterator aIter, * These functions are non-template functions so that users can 1) overload them * with their own types 2) in a way that allows implicit conversions to happen. */ -MOZ_MUST_USE inline HashNumber HashString(const char* aStr) { +[[nodiscard]] inline HashNumber HashString(const char* aStr) { // Use the |const unsigned char*| version of the above so that all ordinary // character data hashes identically. return HashStringUntilZero(reinterpret_cast(aStr)); } -MOZ_MUST_USE inline HashNumber HashString(const char* aStr, size_t aLength) { +[[nodiscard]] inline HashNumber HashString(const char* aStr, size_t aLength) { // Delegate to the |const unsigned char*| version of the above to share // template instantiations. return HashStringKnownLength(reinterpret_cast(aStr), aLength); } -MOZ_MUST_USE -inline HashNumber HashString(const unsigned char* aStr, size_t aLength) { +[[nodiscard]] inline HashNumber HashString(const unsigned char* aStr, + size_t aLength) { return HashStringKnownLength(aStr, aLength); } -MOZ_MUST_USE constexpr HashNumber HashString(const char16_t* aStr) { +[[nodiscard]] constexpr HashNumber HashString(const char16_t* aStr) { return HashStringUntilZero(aStr); } -MOZ_MUST_USE inline HashNumber HashString(const char16_t* aStr, - size_t aLength) { +[[nodiscard]] inline HashNumber HashString(const char16_t* aStr, + size_t aLength) { return HashStringKnownLength(aStr, aLength); } @@ -308,14 +308,14 @@ MOZ_MUST_USE inline HashNumber HashString(const char16_t* aStr, template ::value && !std::is_same::value>::type> -MOZ_MUST_USE inline HashNumber HashString(const WCharT* aStr) { +[[nodiscard]] inline HashNumber HashString(const WCharT* aStr) { return HashStringUntilZero(aStr); } template ::value && !std::is_same::value>::type> -MOZ_MUST_USE inline HashNumber HashString(const WCharT* aStr, size_t aLength) { +[[nodiscard]] inline HashNumber HashString(const WCharT* aStr, size_t aLength) { return HashStringKnownLength(aStr, aLength); } @@ -325,8 +325,8 @@ MOZ_MUST_USE inline HashNumber HashString(const WCharT* 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 HashNumber HashBytes(const void* bytes, - size_t aLength); +[[nodiscard]] extern MFBT_API HashNumber HashBytes(const void* bytes, + size_t aLength); /** * A pseudorandom function mapping 32-bit integers to 32-bit integers. diff --git a/mfbt/HashTable.h b/mfbt/HashTable.h index 415255898f1a..c841e84da38e 100644 --- a/mfbt/HashTable.h +++ b/mfbt/HashTable.h @@ -32,7 +32,7 @@ // - |MallocAllocPolicy| is the default and is usually appropriate; note that // operations (such as insertions) that might cause allocations are // fallible and must be checked for OOM. These checks are enforced by the -// use of MOZ_MUST_USE. +// use of [[nodiscard]]. // // - |InfallibleAllocPolicy| is another possibility; it allows the // abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE(). @@ -212,7 +212,7 @@ class HashMap { // Attempt to reserve enough space to fit at least |aLen| elements. Does // nothing if the map already has sufficient capacity. - MOZ_MUST_USE bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } + [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } // -- Lookups -------------------------------------------------------------- @@ -247,7 +247,7 @@ class HashMap { // Overwrite existing value with |aValue|, or add it if not present. Returns // false on OOM. template - MOZ_MUST_USE bool put(KeyInput&& aKey, ValueInput&& aValue) { + [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) { AddPtr p = lookupForAdd(aKey); if (p) { p->value() = std::forward(aValue); @@ -260,14 +260,14 @@ class HashMap { // Like put(), but slightly faster. Must only be used when the given key is // not already present. (In debug builds, assertions check this.) template - MOZ_MUST_USE bool putNew(KeyInput&& aKey, ValueInput&& aValue) { + [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) { return mImpl.putNew(aKey, std::forward(aKey), std::forward(aValue)); } template - MOZ_MUST_USE bool putNew(const Lookup& aLookup, KeyInput&& aKey, - ValueInput&& aValue) { + [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey, + ValueInput&& aValue) { return mImpl.putNew(aLookup, std::forward(aKey), std::forward(aValue)); } @@ -331,15 +331,15 @@ class HashMap { // Add a key/value. Returns false on OOM. template - MOZ_MUST_USE bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) { + [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) { return mImpl.add(aPtr, std::forward(aKey), std::forward(aValue)); } // See the comment above lookupForAdd() for details. template - MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey, - ValueInput&& aValue) { + [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey, + ValueInput&& aValue) { return mImpl.relookupOrAdd(aPtr, aKey, std::forward(aKey), std::forward(aValue)); } @@ -501,7 +501,7 @@ class HashSet { // Attempt to reserve enough space to fit at least |aLen| elements. Does // nothing if the map already has sufficient capacity. - MOZ_MUST_USE bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } + [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } // -- Lookups -------------------------------------------------------------- @@ -534,7 +534,7 @@ class HashSet { // Add |aU| if it is not present already. Returns false on OOM. template - MOZ_MUST_USE bool put(U&& aU) { + [[nodiscard]] bool put(U&& aU) { AddPtr p = lookupForAdd(aU); return p ? true : add(p, std::forward(aU)); } @@ -542,13 +542,13 @@ class HashSet { // Like put(), but slightly faster. Must only be used when the given element // is not already present. (In debug builds, assertions check this.) template - MOZ_MUST_USE bool putNew(U&& aU) { + [[nodiscard]] bool putNew(U&& aU) { return mImpl.putNew(aU, std::forward(aU)); } // Like the other putNew(), but for when |Lookup| is different to |T|. template - MOZ_MUST_USE bool putNew(const Lookup& aLookup, U&& aU) { + [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) { return mImpl.putNew(aLookup, std::forward(aU)); } @@ -610,13 +610,14 @@ class HashSet { // Add an element. Returns false on OOM. template - MOZ_MUST_USE bool add(AddPtr& aPtr, U&& aU) { + [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) { return mImpl.add(aPtr, std::forward(aU)); } // See the comment above lookupForAdd() for details. template - MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, U&& aU) { + [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, + U&& aU) { return mImpl.relookupOrAdd(aPtr, aLookup, std::forward(aU)); } @@ -2018,7 +2019,7 @@ class HashTable : private AllocPolicy { compact(); } - MOZ_MUST_USE bool reserve(uint32_t aLen) { + [[nodiscard]] bool reserve(uint32_t aLen) { if (aLen == 0) { return true; } @@ -2093,7 +2094,7 @@ class HashTable : private AllocPolicy { } template - MOZ_MUST_USE bool add(AddPtr& aPtr, Args&&... aArgs) { + [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) { ReentrancyGuard g(*this); MOZ_ASSERT_IF(aPtr.isValid(), mTable); MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this); @@ -2165,7 +2166,7 @@ class HashTable : private AllocPolicy { // Note: |aLookup| may be alias arguments in |aArgs|, so this function must // take care not to use |aLookup| after moving |aArgs|. template - MOZ_MUST_USE bool putNew(const Lookup& aLookup, Args&&... aArgs) { + [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) { if (!this->checkSimulatedOOM()) { return false; } @@ -2182,8 +2183,8 @@ class HashTable : private AllocPolicy { // Note: |aLookup| may be a reference to a piece of |u|, so this function // must take care not to use |aLookup| after moving |u|. template - MOZ_MUST_USE bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, - Args&&... aArgs) { + [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, + Args&&... aArgs) { // Check for error from ensureHash() here. if (!aPtr.isLive()) { return false; diff --git a/mfbt/PairHash.h b/mfbt/PairHash.h index 8699e692202a..100832dc1253 100644 --- a/mfbt/PairHash.h +++ b/mfbt/PairHash.h @@ -23,13 +23,13 @@ namespace mozilla { * where type T and U both support AddToHash. */ template -MOZ_MUST_USE inline HashNumber HashPair(const std::pair& pair) { +[[nodiscard]] inline HashNumber HashPair(const std::pair& pair) { // Pair hash combines the hash of each member return HashGeneric(pair.first, pair.second); } template -MOZ_MUST_USE inline HashNumber HashCompactPair(const CompactPair& pair) { +[[nodiscard]] inline HashNumber HashCompactPair(const CompactPair& pair) { // Pair hash combines the hash of each member return HashGeneric(pair.first(), pair.second()); } diff --git a/mfbt/SPSCQueue.h b/mfbt/SPSCQueue.h index 0e116c546831..a4592c0b0f5a 100644 --- a/mfbt/SPSCQueue.h +++ b/mfbt/SPSCQueue.h @@ -123,8 +123,9 @@ class SPSCRingBufferBase { * @param count The number of elements to enqueue. * @return The number of element enqueued. */ - MOZ_MUST_USE - int EnqueueDefault(int aCount) { return Enqueue(nullptr, aCount); } + [[nodiscard]] int EnqueueDefault(int aCount) { + return Enqueue(nullptr, aCount); + } /** * @brief Put an element in the queue. * @@ -134,8 +135,7 @@ class SPSCRingBufferBase { * * @return 1 if the element was inserted, 0 otherwise. */ - MOZ_MUST_USE - int Enqueue(T& aElement) { return Enqueue(&aElement, 1); } + [[nodiscard]] int Enqueue(T& aElement) { return Enqueue(&aElement, 1); } /** * Push `aCount` elements in the ring buffer. * @@ -147,8 +147,7 @@ class SPSCRingBufferBase { * @return The number of elements successfully coped from `elements` and * inserted into the ring buffer. */ - MOZ_MUST_USE - int Enqueue(T* aElements, int aCount) { + [[nodiscard]] int Enqueue(T* aElements, int aCount) { #ifdef DEBUG AssertCorrectThread(mProducerId); #endif @@ -194,8 +193,7 @@ class SPSCRingBufferBase { * @param count The maximum number of elements to Dequeue. * @return The number of elements written to `elements`. */ - MOZ_MUST_USE - int Dequeue(T* elements, int count) { + [[nodiscard]] int Dequeue(T* elements, int count) { #ifdef DEBUG AssertCorrectThread(mConsumerId); #endif diff --git a/mfbt/ScopeExit.h b/mfbt/ScopeExit.h index 90ac05b3d037..cdd5bd9bf155 100644 --- a/mfbt/ScopeExit.h +++ b/mfbt/ScopeExit.h @@ -116,7 +116,7 @@ class MOZ_STACK_CLASS ScopeExit { }; template -MOZ_MUST_USE ScopeExit MakeScopeExit( +[[nodiscard]] ScopeExit MakeScopeExit( ExitFunction&& exitFunction) { return ScopeExit(std::move(exitFunction)); } diff --git a/mfbt/SegmentedVector.h b/mfbt/SegmentedVector.h index 69817b2ec22e..2b7a097726f4 100644 --- a/mfbt/SegmentedVector.h +++ b/mfbt/SegmentedVector.h @@ -156,7 +156,7 @@ class SegmentedVector : private AllocPolicy { // 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) { last = this->template pod_malloc(1); diff --git a/mfbt/ThreadLocal.h b/mfbt/ThreadLocal.h index ab5cd1c35fbd..0a1a203a3a55 100644 --- a/mfbt/ThreadLocal.h +++ b/mfbt/ThreadLocal.h @@ -170,7 +170,7 @@ class ThreadLocalNativeStorage { template class Storage> class ThreadLocal : public Storage { public: - MOZ_MUST_USE inline bool init(); + [[nodiscard]] inline bool init(); void infallibleInit() { MOZ_RELEASE_ASSERT(init(), "Infallible TLS initialization failed"); diff --git a/mfbt/UniquePtr.h b/mfbt/UniquePtr.h index a6de9ead4f26..17cac0b8a80c 100644 --- a/mfbt/UniquePtr.h +++ b/mfbt/UniquePtr.h @@ -292,7 +292,7 @@ class UniquePtr { 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; return p; @@ -403,7 +403,7 @@ class UniquePtr { 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; return p; diff --git a/mfbt/Vector.h b/mfbt/Vector.h index f69e45fb4e9b..c8cc5e2ed4e5 100644 --- a/mfbt/Vector.h +++ b/mfbt/Vector.h @@ -116,7 +116,8 @@ struct VectorImpl { * aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will * not overflow. */ - static inline MOZ_MUST_USE bool growTo(Vector& aV, size_t aNewCap) { + [[nodiscard]] static inline bool growTo(Vector& aV, + size_t aNewCap) { MOZ_ASSERT(!aV.usingInlineStorage()); MOZ_ASSERT(!CapacityHasExcessSpace(aNewCap)); T* newbuf = aV.template pod_malloc(aNewCap); @@ -200,7 +201,8 @@ struct VectorImpl { } } - static inline MOZ_MUST_USE bool growTo(Vector& aV, size_t aNewCap) { + [[nodiscard]] static inline bool growTo(Vector& aV, + size_t aNewCap) { MOZ_ASSERT(!aV.usingInlineStorage()); MOZ_ASSERT(!CapacityHasExcessSpace(aNewCap)); T* newbuf = @@ -252,9 +254,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 */ @@ -576,7 +578,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * 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 @@ -585,7 +587,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * 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 @@ -595,7 +597,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * 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 @@ -610,18 +612,18 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { 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(); @@ -657,29 +659,29 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * not amused.") */ template - MOZ_MUST_USE bool append(U&& aU); + [[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; Impl::new_(&back(), std::forward(aArgs)...); return true; } template - MOZ_MUST_USE bool appendAll(const Vector& aU); + [[nodiscard]] bool appendAll(const Vector& aU); template - MOZ_MUST_USE bool appendAll(Vector&& aU); - MOZ_MUST_USE bool appendN(const T& aT, size_t aN); + [[nodiscard]] bool appendAll(Vector&& aU); + [[nodiscard]] bool appendN(const T& aT, size_t aN); template - MOZ_MUST_USE bool append(const U* aBegin, const U* aEnd); + [[nodiscard]] bool append(const U* aBegin, const U* aEnd); template - MOZ_MUST_USE bool append(const U* aBegin, size_t aLength); + [[nodiscard]] bool append(const U* aBegin, size_t aLength); template - MOZ_MUST_USE bool moveAppend(U* aBegin, U* aEnd); + [[nodiscard]] bool moveAppend(U* aBegin, U* aEnd); /* * Guaranteed-infallible append operations for use upon vectors whose @@ -719,7 +721,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * * 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 @@ -737,7 +739,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * 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 @@ -776,7 +778,7 @@ class MOZ_NON_PARAM Vector final : private AllocPolicy { * 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 0c5375281f23..4cfa19c09ae1 100644 --- a/mfbt/tests/TestSegmentedVector.cpp +++ b/mfbt/tests/TestSegmentedVector.cpp @@ -36,7 +36,7 @@ class InfallibleAllocPolicy { }; // 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.