[libcxx-commits] [libcxx] 91268a5 - [libc++][scoped_allocator] Applied `[[nodiscard]]` (#175291)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jan 12 03:23:16 PST 2026
Author: Hristo Hristov
Date: 2026-01-12T13:23:12+02:00
New Revision: 91268a50f246676479452b0bec36bab88bfe0dcd
URL: https://github.com/llvm/llvm-project/commit/91268a50f246676479452b0bec36bab88bfe0dcd
DIFF: https://github.com/llvm/llvm-project/commit/91268a50f246676479452b0bec36bab88bfe0dcd.diff
LOG: [libc++][scoped_allocator] Applied `[[nodiscard]]` (#175291)
`[[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
Added:
libcxx/test/libcxx/utilities/allocator.adaptor/scoped_allocator.nodiscard.verify.cpp
Modified:
libcxx/include/scoped_allocator
Removed:
libcxx/test/libcxx/diagnostics/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