From fe25426ee4b8fbded17ec83325919dc80b0c1532 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 2 Aug 2019 12:05:09 +0200 Subject: [PATCH] AK: Simplify RefPtr and NonnullRefPtr's leak_ref() functions Use AK::exchange() to switch out the internal storage. Also mark these functions with [[nodiscard]] to provoke an compile-time error if they are called without using the return value. --- AK/NonnullRefPtr.h | 6 ++---- AK/RefPtr.h | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index fcb4bc245a..812457a01a 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -148,14 +148,12 @@ public: return *this; } - CALLABLE_WHEN(unconsumed) + [[nodiscard]] CALLABLE_WHEN(unconsumed) SET_TYPESTATE(consumed) T& leak_ref() { ASSERT(m_ptr); - T* leakedPtr = m_ptr; - m_ptr = nullptr; - return *leakedPtr; + return *exchange(m_ptr, nullptr); } CALLABLE_WHEN("unconsumed","unknown") diff --git a/AK/RefPtr.h b/AK/RefPtr.h index d698cfa261..5fe7a31536 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -188,11 +188,9 @@ public: bool operator!() const { return !m_ptr; } - T* leak_ref() + [[nodiscard]] T* leak_ref() { - T* leakedPtr = m_ptr; - m_ptr = nullptr; - return leakedPtr; + return exchange(m_ptr, nullptr); } T* ptr() { return m_ptr; }