[libcxx-commits] [libcxx] [libc++][memory] Applied `[[nodiscard]]` to more functions (PR #172131)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 25 07:53:00 PST 2025


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/172131

>From 20bbae22fe2525f24f237bdd4b4240eb2c955e69 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Sat, 13 Dec 2025 08:14:12 +0200
Subject: [PATCH] [libc+] Applied `[[nodiscard]]` to Allocators

Towards #172124
---
 libcxx/include/__memory/addressof.h           | 12 ++--
 libcxx/include/__memory/allocator.h           |  7 +-
 .../__memory/is_sufficiently_aligned.h        |  2 +-
 .../include/__memory/raw_storage_iterator.h   |  2 +-
 .../diagnostics/memory.nodiscard.verify.cpp   | 64 +++++++++++++------
 5 files changed, 58 insertions(+), 29 deletions(-)

diff --git a/libcxx/include/__memory/addressof.h b/libcxx/include/__memory/addressof.h
index 667071dfc6635..52ec94a5299fa 100644
--- a/libcxx/include/__memory/addressof.h
+++ b/libcxx/include/__memory/addressof.h
@@ -19,7 +19,8 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Tp>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* addressof(_Tp& __x) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp*
+addressof(_Tp& __x) _NOEXCEPT {
   return __builtin_addressof(__x);
 }
 
@@ -27,24 +28,25 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* a
 // Objective-C++ Automatic Reference Counting uses qualified pointers
 // that require special addressof() signatures.
 template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
   return &__x;
 }
 
 #  if __has_feature(objc_arc_weak)
 template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
   return &__x;
 }
 #  endif
 
 template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
   return &__x;
 }
 
 template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp* addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp*
+addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
   return &__x;
 }
 #endif
diff --git a/libcxx/include/__memory/allocator.h b/libcxx/include/__memory/allocator.h
index 1c96a2ab64578..609b305a1250f 100644
--- a/libcxx/include/__memory/allocator.h
+++ b/libcxx/include/__memory/allocator.h
@@ -120,10 +120,11 @@ class allocator
     typedef allocator<_Up> other;
   };
 
-  _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
     return std::addressof(__x);
   }
-  _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
+  address(const_reference __x) const _NOEXCEPT {
     return std::addressof(__x);
   }
 
@@ -131,7 +132,7 @@ class allocator
     return allocate(__n);
   }
 
-  _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
     return size_type(~0) / sizeof(_Tp);
   }
 
diff --git a/libcxx/include/__memory/is_sufficiently_aligned.h b/libcxx/include/__memory/is_sufficiently_aligned.h
index 4280920cabb4b..93d24aaf78f0b 100644
--- a/libcxx/include/__memory/is_sufficiently_aligned.h
+++ b/libcxx/include/__memory/is_sufficiently_aligned.h
@@ -23,7 +23,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 #if _LIBCPP_STD_VER >= 26
 
 template <size_t _Alignment, class _Tp>
-_LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool is_sufficiently_aligned(_Tp* __ptr) {
   return reinterpret_cast<uintptr_t>(__ptr) % _Alignment == 0;
 }
 
diff --git a/libcxx/include/__memory/raw_storage_iterator.h b/libcxx/include/__memory/raw_storage_iterator.h
index d98b3faf48468..7c9b34076de46 100644
--- a/libcxx/include/__memory/raw_storage_iterator.h
+++ b/libcxx/include/__memory/raw_storage_iterator.h
@@ -67,7 +67,7 @@ class _LIBCPP_DEPRECATED_IN_CXX17 raw_storage_iterator
     return __t;
   }
 #  if _LIBCPP_STD_VER >= 14
-  _LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _OutputIterator base() const { return __x_; }
 #  endif
 };
 
diff --git a/libcxx/test/libcxx/diagnostics/memory.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/memory.nodiscard.verify.cpp
index 6410c84e926aa..056cf933b28ce 100644
--- a/libcxx/test/libcxx/diagnostics/memory.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/memory.nodiscard.verify.cpp
@@ -6,37 +6,63 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03
+// <memory>
 
-// check that <memory> functions are marked [[nodiscard]]
+// Check that functions are marked [[nodiscard]]
 
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_RAW_STORAGE_ITERATOR
 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_TEMPORARY_BUFFER
 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
 
-// clang-format off
-
 #include <memory>
 
 #include "test_macros.h"
 
 void test() {
-  std::get_temporary_buffer<int>(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-}
+  int i = 0;
 
-void test_allocator_traits() {
-  std::allocator<int> allocator;
-  std::allocator_traits<std::allocator<int>> allocator_traits;
-  allocator_traits.allocate(allocator, 1);          // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-  allocator_traits.allocate(allocator, 1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-}
+  {
+    std::addressof(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::get_temporary_buffer<int>(0);
+#if _LIBCPP_STD_VER >= 26
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::is_sufficiently_aligned<2>(&i);
+#endif
+  }
 
-void test_allocator() {
   std::allocator<int> allocator;
-  allocator.allocate(1);          // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-#if TEST_STD_VER <= 17
-  allocator.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-#endif
+
+  {
+    std::allocator_traits<std::allocator<int>> allocator_traits;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    allocator_traits.allocate(allocator, 1);
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    allocator_traits.allocate(allocator, 1, nullptr);
+  }
+
 #if TEST_STD_VER >= 23
-  allocator.allocate_at_least(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  allocator.allocate_at_least(1);
+#endif
+
+  { // Deprecated in C++17. Removed in C++20.
+    const int ci = 0;
+
+    allocator.address(i);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+    allocator.address(ci); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+    allocator.allocate(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    allocator.allocate(1, nullptr);
+    allocator.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  }
+
+#if TEST_STD_VER >= 14
+  {
+    std::raw_storage_iterator<int*, int> it{nullptr};
+
+    it.base(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  }
 #endif
-}
+}
\ No newline at end of file



More information about the libcxx-commits mailing list