[libcxx-commits] [libcxx] [libc++] mark `std::expected` as `nodiscard` (PR #130820)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Mar 14 07:56:09 PDT 2025
================
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+
+// <expected>
+
+// Test that ignoring 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();
----------------
frederick-vs-ja wrote:
I think we can add some _well-formed_ function calls to these files and expect the warnings.
- `libcxx/utilities/expected/expected.expected/and_then.mandates.verify.cpp`
- `libcxx/utilities/expected/expected.expected/or_else.mandates.verify.cpp`
- `libcxx/utilities/expected/expected.void/and_then.mandates.verify.cpp`
- `libcxx/utilities/expected/expected.void/or_else.mandates.verify.cpp`
https://github.com/llvm/llvm-project/pull/130820
More information about the libcxx-commits
mailing list