[libcxx-commits] [libcxx] [libc++][scoped_allocator] Applied `[[nodiscard]]` (PR #175291)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 9 22:53:05 PST 2026


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

>From 4441b184961c372e61a4cde88471ad783fc1b991 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Sat, 10 Jan 2026 08:38:56 +0200
Subject: [PATCH] [libc++][scoped_allocator] Applied `[[nodiscard]]`

[[nodiscard]] should be applied to functions where discarding the return value is most likely a correctness issue.

- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/allocator.adaptor

Towards #172124
---
 libcxx/include/scoped_allocator               | 17 +++++---
 .../scoped_allocator.nodiscard.verify.cpp     | 22 -----------
 .../scoped_allocator.nodiscard.verify.cpp     | 39 +++++++++++++++++++
 3 files changed, 50 insertions(+), 28 deletions(-)
 delete mode 100644 libcxx/test/libcxx/diagnostics/scoped_allocator.nodiscard.verify.cpp
 create mode 100644 libcxx/test/libcxx/utilities/allocator.adaptor/scoped_allocator.nodiscard.verify.cpp

diff --git a/libcxx/include/scoped_allocator b/libcxx/include/scoped_allocator
index c72c470f0c541..a469d4afea245 100644
--- a/libcxx/include/scoped_allocator
+++ b/libcxx/include/scoped_allocator
@@ -382,13 +382,17 @@ public:
   // scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default;
   // ~scoped_allocator_adaptor() = default;
 
-  _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return _Base::inner_allocator(); }
-  _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT {
+    return _Base::inner_allocator();
+  }
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT {
     return _Base::inner_allocator();
   }
 
-  _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT { return _Base::outer_allocator(); }
-  _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT {
+    return _Base::outer_allocator();
+  }
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT {
     return _Base::outer_allocator();
   }
 
@@ -403,7 +407,7 @@ public:
     allocator_traits<outer_allocator_type>::deallocate(outer_allocator(), __p, __n);
   }
 
-  _LIBCPP_HIDE_FROM_ABI size_type max_size() const {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const {
     return allocator_traits<outer_allocator_type>::max_size(outer_allocator());
   }
 
@@ -473,7 +477,8 @@ public:
     allocator_traits<typename _OM::type>::destroy(_OM()(outer_allocator()), __p);
   }
 
-  _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor select_on_container_copy_construction() const _NOEXCEPT {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor
+  select_on_container_copy_construction() const _NOEXCEPT {
     return _Base::select_on_container_copy_construction();
   }
 
diff --git a/libcxx/test/libcxx/diagnostics/scoped_allocator.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/scoped_allocator.nodiscard.verify.cpp
deleted file mode 100644
index 5c5360c412089..0000000000000
--- a/libcxx/test/libcxx/diagnostics/scoped_allocator.nodiscard.verify.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03
-
-// check that <scoped_allocator> functions are marked [[nodiscard]]
-
-// clang-format off
-
-#include <memory>
-#include <scoped_allocator>
-
-void test() {
-  std::scoped_allocator_adaptor<std::allocator<int>> alloc;
-  alloc.allocate(1);          // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-  alloc.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-}
diff --git a/libcxx/test/libcxx/utilities/allocator.adaptor/scoped_allocator.nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/allocator.adaptor/scoped_allocator.nodiscard.verify.cpp
new file mode 100644
index 0000000000000..1d06771d71cce
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/allocator.adaptor/scoped_allocator.nodiscard.verify.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++11
+
+// <scoped_allocator>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <memory>
+#include <scoped_allocator>
+
+void test() {
+  std::scoped_allocator_adaptor<std::allocator<int>> alloc;
+  const std::scoped_allocator_adaptor<std::allocator<int>> cAlloc;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  alloc.inner_allocator();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cAlloc.inner_allocator();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  alloc.outer_allocator();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cAlloc.outer_allocator();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  alloc.allocate(1);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  alloc.allocate(1, nullptr);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cAlloc.max_size();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cAlloc.select_on_container_copy_construction();
+}



More information about the libcxx-commits mailing list