[libcxx-commits] [libcxx] [libc++][type_traits] Applied `[[nodiscard]]` (PR #200760)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 1 02:07:00 PDT 2026


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/200760

`[[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/type.traits

>From aaa54cd3c90c21d6109badf4cea4eb9ec4d8b775 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Mon, 1 Jun 2026 16:28:17 +0800
Subject: [PATCH] [libc++][type_traits] Applied `[[nodiscard]]`

`[[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/type.traits
---
 .../include/__type_traits/integral_constant.h |  2 +-
 .../__type_traits/is_constant_evaluated.h     |  2 +-
 .../__type_traits/is_within_lifetime.h        |  2 +-
 .../libcxx/type_traits/nodiscard.verify.cpp   | 33 +++++++++++++++++++
 4 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 libcxx/test/libcxx/type_traits/nodiscard.verify.cpp

diff --git a/libcxx/include/__type_traits/integral_constant.h b/libcxx/include/__type_traits/integral_constant.h
index ff55a85e0d38a..5eb6dae898595 100644
--- a/libcxx/include/__type_traits/integral_constant.h
+++ b/libcxx/include/__type_traits/integral_constant.h
@@ -24,7 +24,7 @@ struct _LIBCPP_NO_SPECIALIZATIONS integral_constant {
   typedef integral_constant type;
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR operator value_type() const _NOEXCEPT { return value; }
 #if _LIBCPP_STD_VER >= 14
-  _LIBCPP_HIDE_FROM_ABI constexpr value_type operator()() const _NOEXCEPT { return value; }
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr value_type operator()() const _NOEXCEPT { return value; }
 #endif
 };
 
diff --git a/libcxx/include/__type_traits/is_constant_evaluated.h b/libcxx/include/__type_traits/is_constant_evaluated.h
index 05e070a747884..51b5b95ade4cd 100644
--- a/libcxx/include/__type_traits/is_constant_evaluated.h
+++ b/libcxx/include/__type_traits/is_constant_evaluated.h
@@ -18,7 +18,7 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool is_constant_evaluated() noexcept {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_constant_evaluated() noexcept {
   return __builtin_is_constant_evaluated();
 }
 #endif
diff --git a/libcxx/include/__type_traits/is_within_lifetime.h b/libcxx/include/__type_traits/is_within_lifetime.h
index 242f2adaf357b..c789481cbd124 100644
--- a/libcxx/include/__type_traits/is_within_lifetime.h
+++ b/libcxx/include/__type_traits/is_within_lifetime.h
@@ -19,7 +19,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 26 && __has_builtin(__builtin_is_within_lifetime)
 template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI consteval bool is_within_lifetime(const _Tp* __p) noexcept {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI consteval bool is_within_lifetime(const _Tp* __p) noexcept {
   return __builtin_is_within_lifetime(__p);
 }
 #endif
diff --git a/libcxx/test/libcxx/type_traits/nodiscard.verify.cpp b/libcxx/test/libcxx/type_traits/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..7c0f45700b4a2
--- /dev/null
+++ b/libcxx/test/libcxx/type_traits/nodiscard.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++14
+
+// Check that functions are marked [[nodiscard]]
+
+#include <type_traits>
+
+#include "test_macros.h"
+
+void test() {
+  std::true_type tag;
+  tag(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if TEST_STD_VER >= 20
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::is_constant_evaluated();
+#endif
+}
+
+#if TEST_STD_VER >= 26 && defined(__cpp_lib_is_within_lifetime)
+consteval void test_consteval() {
+  [[maybe_unused]] int n = 0;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::is_within_lifetime(&n);
+}
+#endif



More information about the libcxx-commits mailing list