[libcxx-commits] [libcxx] [libc++][utility] Test `[[nodiscard]] std::exchange` (PR #195807)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 5 01:45:23 PDT 2026


https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/195807

1. `[[nodiscard]]` was applied in https://llvm.org/PR187953
2. Also use `[[__nodiscard__]]` in pre-C++17 code.

References:
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- https://wg21.link/utility.exchange

>From 37ac7e2cdf33f684b71ba69d01c8e3bb38a4920a Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 5 May 2026 11:41:18 +0300
Subject: [PATCH] [libc++][utility] Test `[[nodiscard]] std::exchange`

`[[nodiscard]]` was applied in
https://llvm.org/PR187953
Also use `[[__nodiscard__]]` in pre-C++17 code.

References:
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- https://wg21.link/utility.exchange
---
 libcxx/include/__utility/exchange.h                       | 5 +++--
 libcxx/test/libcxx/utilities/utility/nodiscard.verify.cpp | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__utility/exchange.h b/libcxx/include/__utility/exchange.h
index 368818c5541bd..c8af46f2a7d6b 100644
--- a/libcxx/include/__utility/exchange.h
+++ b/libcxx/include/__utility/exchange.h
@@ -28,7 +28,7 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _T1, class _T2 = _T1>
-[[nodiscard]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1
+[[__nodiscard__]] _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _T1
 __exchange(_T1& __obj, _T2&& __new_value)
     _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value&& is_nothrow_assignable<_T1&, _T2>::value) {
   _T1 __old_value = std::move(__obj);
@@ -38,7 +38,8 @@ __exchange(_T1& __obj, _T2&& __new_value)
 
 #if _LIBCPP_STD_VER >= 14
 template <class _T1, class _T2 = _T1>
-[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _T1 exchange(_T1& __obj, _T2&& __new_value) noexcept(
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
+_LIBCPP_CONSTEXPR_SINCE_CXX20 _T1 exchange(_T1& __obj, _T2&& __new_value) noexcept(
     is_nothrow_move_constructible<_T1>::value && is_nothrow_assignable<_T1&, _T2>::value) {
   return std::__exchange(__obj, std::forward<_T2>(__new_value));
 }
diff --git a/libcxx/test/libcxx/utilities/utility/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/utility/nodiscard.verify.cpp
index 3d6ff423dc917..c7426ba8ae711 100644
--- a/libcxx/test/libcxx/utilities/utility/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/utility/nodiscard.verify.cpp
@@ -15,6 +15,7 @@
 void test() {
   int i = 0;
 
+  std::exchange(i, 1);  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::forward<int>(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::forward<int>(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::move(i);         // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}



More information about the libcxx-commits mailing list