[libcxx-commits] [libcxx] [libc++] mark `std::expected` as `nodiscard` (PR #130820)

Mohamed Emad via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 12 06:26:04 PDT 2025


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/130820

>From 5ef0eb9e5159c6d04eefb24a1afe69a9a3c83e50 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 11 Mar 2025 21:30:25 +0200
Subject: [PATCH 1/3] [libc++] mark `std::expected` as `nodiscard` (fix
 #130656)

---
 libcxx/include/__cxx03/__expected/expected.h | 2 +-
 libcxx/include/__expected/expected.h         | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__cxx03/__expected/expected.h b/libcxx/include/__cxx03/__expected/expected.h
index 1d54bb9f6edeb..ce9676c68c2dc 100644
--- a/libcxx/include/__cxx03/__expected/expected.h
+++ b/libcxx/include/__cxx03/__expected/expected.h
@@ -58,7 +58,7 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Tp, class _Err>
-class expected;
+class [[nodiscrad]] expected;
 
 template <class _Tp>
 struct __is_std_expected : false_type {};
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 03bbd1623ed5c..8dd32668bcb33 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -60,7 +60,7 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Tp, class _Err>
-class expected;
+class [[nodiscard]] expected;
 
 template <class _Tp>
 struct __is_std_expected : false_type {};

>From 5943a160fdf8e48a3c2ec20d12000f652e3794b8 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Tue, 11 Mar 2025 23:13:59 +0200
Subject: [PATCH 2/3] [libcxx] unit test for verifing `nodiscard` for
 `std::expected`

---
 .../expected.expected/nodiscard.verify.cpp    | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp

diff --git a/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..c8248e7cf32cb
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// <expected>
+
+// Test that std::expected generates [[nodiscard]] warnings
+
+#include <expected>
+
+std::expected<int, int> returns_expected() {
+  return std::expected<int, int>(5);
+}
+
+std::expected<void, int> returns_expected_void() {
+  return std::expected<void, int>();
+}
+
+void test() {
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  returns_expected();
+  
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  returns_expected_void();
+}
\ No newline at end of file

>From 9c7a735583566ff7343e98272cac39a95cd5b471 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 12 Mar 2025 15:25:47 +0200
Subject: [PATCH 3/3] [libcxx] [test] improve `expected/nodiscrard.verify`

---
 .../expected/expected.expected/nodiscard.verify.cpp         | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
index c8248e7cf32cb..5c3263ec53ca9 100644
--- a/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
@@ -6,11 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14, c++17
+// REQUIRES: std-at-least-c++23
 
 // <expected>
 
-// Test that std::expected generates [[nodiscard]] warnings
+// Test that ignoring std::expected generates [[nodiscard]] warnings.
 
 #include <expected>
 
@@ -28,4 +28,4 @@ void test() {
   
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   returns_expected_void();
-}
\ No newline at end of file
+}



More information about the libcxx-commits mailing list