From 5013a6480d989b48c9a910cfe3c4ad19f0508597 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 1 Dec 2021 21:05:13 +0000 Subject: [PATCH] AK: Mark smart pointer classes as [[nodiscard]] This makes it an error to not do something with a returned smart pointer, which should help prevent mistakes. In cases where you do need to ignore the value, casting to void will placate the compiler. I did have to add comments to disable clang-format on a couple of lines, where it wanted to format the code like this: ```c++ private : NonnullRefPtr() = delete; ``` --- AK/NonnullOwnPtr.h | 2 +- AK/NonnullRefPtr.h | 5 ++++- AK/OwnPtr.h | 2 +- AK/RefPtr.h | 2 +- AK/WeakPtr.h | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 1645a0009d..8121467e5b 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -25,7 +25,7 @@ template class WeakPtr; template -class NonnullOwnPtr { +class [[nodiscard]] NonnullOwnPtr { public: using ElementType = T; diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 7f9a0d00c2..4640518c2a 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -39,7 +39,7 @@ ALWAYS_INLINE void unref_if_not_null(T* ptr) } template -class NonnullRefPtr { +class [[nodiscard]] NonnullRefPtr { template friend class RefPtr; template @@ -218,8 +218,11 @@ public: AK::swap(m_ptr, other.m_ptr); } + // clang-format off private: NonnullRefPtr() = delete; + // clang-format on + ALWAYS_INLINE RETURNS_NONNULL T* as_nonnull_ptr() const { VERIFY(m_ptr); diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index b950076efc..fe1494bf8d 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -15,7 +15,7 @@ namespace AK { template -class OwnPtr { +class [[nodiscard]] OwnPtr { public: OwnPtr() = default; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 41fe032d2b..5d3acff198 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -27,7 +27,7 @@ template class OwnPtr; template -class RefPtr { +class [[nodiscard]] RefPtr { template friend class RefPtr; template diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h index 69c36d2428..f31004f7cd 100644 --- a/AK/WeakPtr.h +++ b/AK/WeakPtr.h @@ -15,7 +15,7 @@ namespace AK { template -class WeakPtr { +class [[nodiscard]] WeakPtr { template friend class Weakable;