[libcxx-commits] [libcxx] [libc++][optional] Applied `[[nodiscard]]` (PR #170045)

William Tran-Viet via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 1 05:16:53 PST 2025


================
@@ -0,0 +1,156 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++17
+
+// <optional>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <string>
+#include <optional>
+#include <utility>
+
+#include "test_macros.h"
+
+struct LVal {
+  constexpr std::optional<int> operator()(int&) { return 1; }
+  std::optional<int> operator()(const int&)  = delete;
+  std::optional<int> operator()(int&&)       = delete;
+  std::optional<int> operator()(const int&&) = delete;
+};
+
+struct CLVal {
+  std::optional<int> operator()(int&) = delete;
+  constexpr std::optional<int> operator()(const int&) { return 1; }
+  std::optional<int> operator()(int&&)       = delete;
+  std::optional<int> operator()(const int&&) = delete;
+};
+
+struct RVal {
+  std::optional<int> operator()(int&)       = delete;
+  std::optional<int> operator()(const int&) = delete;
+  constexpr std::optional<int> operator()(int&&) { return 1; }
+  std::optional<int> operator()(const int&&) = delete;
+};
+
+struct CRVal {
+  std::optional<int> operator()(int&)       = delete;
+  std::optional<int> operator()(const int&) = delete;
+  std::optional<int> operator()(int&&)      = delete;
+  constexpr std::optional<int> operator()(const int&&) { return 1; }
+};
+
+void test() {
+  std::bad_optional_access ex;
+
+  ex.what(); // expect-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
----------------
smallp-o-p wrote:


```suggestion
  ex.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
```


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


More information about the libcxx-commits mailing list