[libcxx-commits] [libcxx] [libc++] Apply `[[nodiscard]]` to `get_new_handler` (PR #220847)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 3 02:02:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

`[[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/get.new.handler

---
Full diff: https://github.com/llvm/llvm-project/pull/220847.diff


2 Files Affected:

- (modified) libcxx/include/__new/new_handler.h (+1-1) 
- (modified) libcxx/test/libcxx/language.support/nodiscard.verify.cpp (+3) 


``````````diff
diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index c4473c13fab21..5c21c8419fc8a 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -20,7 +20,7 @@ _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
 typedef void (*new_handler)();
 _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
-_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
 
 _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
diff --git a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
index 975eb59a7d52d..ebcf9f40cb6b3 100644
--- a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
@@ -223,6 +223,9 @@ void test() {
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
     ::operator new[](0, std::align_val_t{1}, std::nothrow);
 #endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::get_new_handler();
   }
 
 #if TEST_STD_VER >= 17

``````````

</details>


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


More information about the libcxx-commits mailing list