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

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 3 09:58:43 PDT 2026


Author: A. Jiang
Date: 2026-09-04T00:58:37+08:00
New Revision: 97dc989bb446ba476f71dd4cc36641c3ed186bce

URL: https://github.com/llvm/llvm-project/commit/97dc989bb446ba476f71dd4cc36641c3ed186bce
DIFF: https://github.com/llvm/llvm-project/commit/97dc989bb446ba476f71dd4cc36641c3ed186bce.diff

LOG: [libc++] Apply `[[nodiscard]]` to `get_new_handler` (#220847)

`[[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

Added: 
    

Modified: 
    libcxx/include/__new/new_handler.h
    libcxx/test/libcxx/language.support/nodiscard.verify.cpp

Removed: 
    


################################################################################
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


        


More information about the libcxx-commits mailing list