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

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 21 17:34:39 PST 2025


================
@@ -31,12 +38,28 @@ void test_allocator_traits() {
 }
 
 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
+  [[maybe_unused]] std::allocator<int> allocator;
+
 #if TEST_STD_VER >= 23
   allocator.allocate_at_least(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 #endif
+
+#if TEST_STD_VER <= 17
+  int i = 0;
+  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}}
+  allocator.allocate(1, nullptr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  allocator.max_size();              // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
 }
+
+#if TEST_STD_VER >= 14
----------------
frederick-vs-ja wrote:

Was this really intended?

```suggestion
#if TEST_STD_VER >= 14 && TEST_STD_VER <= 17
```

Also, I found that this `base` member function was added via [LWG2454](https://cplusplus.github.io/LWG/issue2454). So perhaps it would make sense to make `base` available in all old modes (not requested for this PR).

https://github.com/llvm/llvm-project/pull/172131


More information about the libcxx-commits mailing list