[libcxx-commits] [libcxx] [libc++][span] Test `[[nodiscard]]` applied to `span::iterator` (PR #202068)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jun 6 11:41:17 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

<details>
<summary>Changes</summary>

Adds test coverage.

`[[nodicard]]` applied in:
- https://github.com/llvm/llvm-project/pull/198489
- https://github.com/llvm/llvm-project/pull/198492

Towards #<!-- -->172124

---
Full diff: https://github.com/llvm/llvm-project/pull/202068.diff


1 Files Affected:

- (added) libcxx/test/libcxx/containers/views/views.span/nodiscard.iterator.verify.cpp (+43) 


``````````diff
diff --git a/libcxx/test/libcxx/containers/views/views.span/nodiscard.iterator.verify.cpp b/libcxx/test/libcxx/containers/views/views.span/nodiscard.iterator.verify.cpp
new file mode 100644
index 0000000000000..f7ead979c8737
--- /dev/null
+++ b/libcxx/test/libcxx/containers/views/views.span/nodiscard.iterator.verify.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++20
+
+// <span>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <span>
+
+#include "test_macros.h"
+
+void test() {
+  using Container = std::span<int, 82>;
+
+  int arr[82] = {};
+  Container c{arr};
+  Container::iterator it = c.begin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  *it;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it[0];
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it + 1;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  1 + it;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - 1;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - it;
+}

``````````

</details>


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


More information about the libcxx-commits mailing list