[libcxx-commits] [libcxx] [libc++] Applied `[[nodiscard]]` to `array::iterator` (PR #198492)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 27 23:08:39 PDT 2026


================
@@ -0,0 +1,72 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <array>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <array>
+
+#include "test_macros.h"
+
+void test() {
+  typedef std::array<int, 94> Container;
+  Container c;
+  Container::iterator it = c.begin();
+
+#if defined(_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY)
+  ASSERT_SAME_TYPE(Container::iterator, std::__static_bounded_iter<int*, 94>);
+#elif defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY)
+  ASSERT_SAME_TYPE(Container::iterator, std::__wrap_iter<int*>);
+#else
+  ASSERT_SAME_TYPE(Container::iterator, int*);
+#endif
+
+#if defined(_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY) || defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY)
+  // 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;
+#else
+  // expected-warning at +1 {{expression result unused}}
+  *it;
+
+  // expected-warning at +1 {{expression result unused}}
+  it[0];
+
+  // expected-warning at +1 {{expression result unused}}
+  it + 1;
+
+  // expected-warning at +1 {{expression result unused}}
+  1 + it;
+
+  // expected-warning at +1 {{expression result unused}}
+  it - 1;
+
+  // expected-warning at +1 {{expression result unused}}
+  it - it;
+#endif
+
+#if defined(_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY)
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::pointer_traits<Container::iterator>::to_address(it);
----------------
frederick-vs-ja wrote:

The `pointer_traits<__bounded_iter>` partial specialization (and especially its `to_address` function) appears to be necessary, because `std::to_address` need to accept past-of-end iterators in order to make `__bounded_iter` model `contiguous_iterator`, and `__bounded_iter::operator->` rejects past-of-end iterators.

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


More information about the libcxx-commits mailing list