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

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 11 18:11:13 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
+//
+//===----------------------------------------------------------------------===//
+
+// 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();
+}
----------------
frederick-vs-ja wrote:

It seems conventional to have a empty line at the end of a test file.

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


More information about the libcxx-commits mailing list