From 0de403fede6539a8c89ed6fcca97123503d0f455 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Thu, 24 Oct 2024 22:36:28 +0200 Subject: [PATCH] AK+LibJS: Add `[[nodiscard]]` to `operator*` in common types The order of precedence with the `*` operator sometimes makes it a bit harder to detect whether or not the result is actually used. Let's fail compilation if anyone tries to discard the result. --- AK/Iterator.h | 4 ++-- AK/NonnullOwnPtr.h | 2 +- AK/NonnullRefPtr.h | 2 +- AK/Optional.h | 4 ++-- AK/OwnPtr.h | 2 +- AK/RefPtr.h | 2 +- Userland/Libraries/LibJS/Heap/GCPtr.h | 4 ++-- Userland/Libraries/LibJS/Heap/Handle.h | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/AK/Iterator.h b/AK/Iterator.h index 6c7a6fd2464..7b49c01a61d 100644 --- a/AK/Iterator.h +++ b/AK/Iterator.h @@ -76,12 +76,12 @@ public: return SimpleIterator { *m_container, m_index + 1 }; } - ALWAYS_INLINE constexpr ValueType const& operator*() const + [[nodiscard]] ALWAYS_INLINE constexpr ValueType const& operator*() const requires(IsConst) { return (*m_container)[m_index]; } - ALWAYS_INLINE constexpr ValueType& operator*() const + [[nodiscard]] ALWAYS_INLINE constexpr ValueType& operator*() const requires(!IsConst) { return (*m_container)[m_index]; diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 62af8d7dfbd..2fc212bad72 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -101,7 +101,7 @@ public: ALWAYS_INLINE RETURNS_NONNULL T* operator->() const { return ptr(); } - ALWAYS_INLINE T& operator*() const { return *ptr(); } + [[nodiscard]] ALWAYS_INLINE T& operator*() const { return *ptr(); } ALWAYS_INLINE RETURNS_NONNULL operator T*() const { return ptr(); } diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index f3c14adf4ee..62a049c134f 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -168,7 +168,7 @@ public: return as_nonnull_ptr(); } - ALWAYS_INLINE T& operator*() const + [[nodiscard]] ALWAYS_INLINE T& operator*() const { return *as_nonnull_ptr(); } diff --git a/AK/Optional.h b/AK/Optional.h index d8df8a39be1..62b2a8303e2 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -274,8 +274,8 @@ public: return TRY(callback()); } - ALWAYS_INLINE T const& operator*() const { return value(); } - ALWAYS_INLINE T& operator*() { return value(); } + [[nodiscard]] ALWAYS_INLINE T const& operator*() const { return value(); } + [[nodiscard]] ALWAYS_INLINE T& operator*() { return value(); } ALWAYS_INLINE T const* operator->() const { return &value(); } ALWAYS_INLINE T* operator->() { return &value(); } diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index 7d2af97bb06..3d6e3650893 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -140,7 +140,7 @@ public: return m_ptr; } - T& operator*() const + [[nodiscard]] T& operator*() const { VERIFY(m_ptr); return *m_ptr; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 1aae365bf3c..4276b82b786 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -244,7 +244,7 @@ public: return as_nonnull_ptr(); } - ALWAYS_INLINE T& operator*() const + [[nodiscard]] ALWAYS_INLINE T& operator*() const { return *as_nonnull_ptr(); } diff --git a/Userland/Libraries/LibJS/Heap/GCPtr.h b/Userland/Libraries/LibJS/Heap/GCPtr.h index b5d25d57c0b..2ddef6058da 100644 --- a/Userland/Libraries/LibJS/Heap/GCPtr.h +++ b/Userland/Libraries/LibJS/Heap/GCPtr.h @@ -62,7 +62,7 @@ public: RETURNS_NONNULL T* operator->() const { return m_ptr; } - T& operator*() const { return *m_ptr; } + [[nodiscard]] T& operator*() const { return *m_ptr; } RETURNS_NONNULL T* ptr() const { return m_ptr; } @@ -169,7 +169,7 @@ public: return m_ptr; } - T& operator*() const + [[nodiscard]] T& operator*() const { ASSERT(m_ptr); return *m_ptr; diff --git a/Userland/Libraries/LibJS/Heap/Handle.h b/Userland/Libraries/LibJS/Heap/Handle.h index 55a2d082520..b9d579cf8d6 100644 --- a/Userland/Libraries/LibJS/Heap/Handle.h +++ b/Userland/Libraries/LibJS/Heap/Handle.h @@ -96,7 +96,7 @@ public: return cell(); } - T& operator*() const + [[nodiscard]] T& operator*() const { return *cell(); }