[libcxx-commits] [libcxx] [libc++] Applied `[[nodiscard]]` to `__static_bounded_iter` (PR #198492)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Tue May 19 03:53:18 PDT 2026
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/198492
Towards #172124
>From e62101e6753bbbb0c711871f1f0219883e96cf7f Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 19 May 2026 13:52:53 +0300
Subject: [PATCH] [libc++] Applied `[[nodiscard]]` to `__static_bounded_iter`
Towards #172124
---
.../include/__iterator/static_bounded_iter.h | 15 +++---
...nodiscard.__static_bounded_iter.verify.cpp | 47 +++++++++++++++++++
2 files changed, 55 insertions(+), 7 deletions(-)
create mode 100644 libcxx/test/libcxx/containers/sequences/array/nodiscard.__static_bounded_iter.verify.cpp
diff --git a/libcxx/include/__iterator/static_bounded_iter.h b/libcxx/include/__iterator/static_bounded_iter.h
index d8fc7d185e7bc..9663820109670 100644
--- a/libcxx/include/__iterator/static_bounded_iter.h
+++ b/libcxx/include/__iterator/static_bounded_iter.h
@@ -128,7 +128,7 @@ struct __static_bounded_iter {
public:
// Dereference and indexing operations.
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__current() != __end(), "__static_bounded_iter::operator*: Attempt to dereference an iterator at the end");
return *__current();
@@ -140,7 +140,8 @@ struct __static_bounded_iter {
return std::__to_address(__current());
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
+ operator[](difference_type __n) const _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__n >= __begin() - __current(),
"__static_bounded_iter::operator[]: Attempt to index an iterator past the start");
@@ -186,13 +187,13 @@ struct __static_bounded_iter {
__current() += __n;
return *this;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
operator+(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
__static_bounded_iter __tmp(__self);
__tmp += __n;
return __tmp;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
operator+(difference_type __n, __static_bounded_iter const& __self) _NOEXCEPT {
__static_bounded_iter __tmp(__self);
__tmp += __n;
@@ -208,13 +209,13 @@ struct __static_bounded_iter {
__current() -= __n;
return *this;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
operator-(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
__static_bounded_iter __tmp(__self);
__tmp -= __n;
return __tmp;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
operator-(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
return __x.__current() - __y.__current();
}
@@ -306,7 +307,7 @@ struct pointer_traits<__static_bounded_iter<_Iterator, _Size> > {
using element_type = typename pointer_traits<_Iterator>::element_type;
using difference_type = typename pointer_traits<_Iterator>::difference_type;
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
return std::__to_address(__it.__current());
}
};
diff --git a/libcxx/test/libcxx/containers/sequences/array/nodiscard.__static_bounded_iter.verify.cpp b/libcxx/test/libcxx/containers/sequences/array/nodiscard.__static_bounded_iter.verify.cpp
new file mode 100644
index 0000000000000..95e43cb7b9b86
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/array/nodiscard.__static_bounded_iter.verify.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ABI_BOUNDED_ITERATORS_IN_STD_ARRAY
+
+// <array>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <array>
+
+#include "test_macros.h"
+
+void test() { // __static_bounded_iter
+ typedef std::array<int, 94> Container;
+ ASSERT_SAME_TYPE(Container::iterator, std::__static_bounded_iter<int*, 94>);
+
+ Container::iterator it;
+
+ // 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;
+
+ std::pointer_traits<Container::iterator> pt;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ pt.to_address(it);
+}
More information about the libcxx-commits
mailing list