[libcxx-commits] [libcxx] 184a8f9 - [libc++] Applied `[[nodiscard]]` to `array::iterator` (#198492)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu May 28 12:00:04 PDT 2026
Author: Hristo Hristov
Date: 2026-05-28T21:59:59+03:00
New Revision: 184a8f94757c35deecf7a7323ca3ce73c9f228b7
URL: https://github.com/llvm/llvm-project/commit/184a8f94757c35deecf7a7323ca3ce73c9f228b7
DIFF: https://github.com/llvm/llvm-project/commit/184a8f94757c35deecf7a7323ca3ce73c9f228b7.diff
LOG: [libc++] Applied `[[nodiscard]]` to `array::iterator` (#198492)
Towards #172124
Added:
libcxx/test/libcxx/containers/sequences/array/nodiscard.iterator.verify.cpp
Modified:
libcxx/include/__iterator/static_bounded_iter.h
libcxx/include/__iterator/wrap_iter.h
Removed:
################################################################################
diff --git a/libcxx/include/__iterator/static_bounded_iter.h b/libcxx/include/__iterator/static_bounded_iter.h
index d8fc7d185e7bc..a048073b5a897 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[](
diff erence_type __n) const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
+ operator[](
diff erence_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,
diff erence_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+(
diff erence_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,
diff erence_type __n) _NOEXCEPT {
__static_bounded_iter __tmp(__self);
__tmp -= __n;
return __tmp;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend
diff erence_type
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend
diff erence_type
operator-(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
return __x.__current() - __y.__current();
}
diff --git a/libcxx/include/__iterator/wrap_iter.h b/libcxx/include/__iterator/wrap_iter.h
index 0633794e15080..b823a636d38d5 100644
--- a/libcxx/include/__iterator/wrap_iter.h
+++ b/libcxx/include/__iterator/wrap_iter.h
@@ -55,7 +55,9 @@ class __wrap_iter {
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
: __i_(__u.__i_) {}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
+ return *__i_;
+ }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
return std::__to_address(__i_);
}
@@ -78,7 +80,8 @@ class __wrap_iter {
--(*this);
return __tmp;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator+(
diff erence_type __n) const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
+ operator+(
diff erence_type __n) const _NOEXCEPT {
__wrap_iter __w(*this);
__w += __n;
return __w;
@@ -87,14 +90,16 @@ class __wrap_iter {
__i_ += __n;
return *this;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator-(
diff erence_type __n) const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
+ operator-(
diff erence_type __n) const _NOEXCEPT {
return *this + (-__n);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(
diff erence_type __n) _NOEXCEPT {
*this += -__n;
return *this;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](
diff erence_type __n) const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
+ operator[](
diff erence_type __n) const _NOEXCEPT {
return __i_[__n];
}
@@ -201,18 +206,18 @@ class __wrap_iter {
#ifndef _LIBCPP_CXX03_LANG
template <class _Iter2>
- _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 auto
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 auto
operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.__i_ - __y.__i_)
#else
template <class _Iter2>
- _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14
typename __wrap_iter::
diff erence_type operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
#endif // C++03
{
return __x.__i_ - __y.__i_;
}
- _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
operator+(typename __wrap_iter::
diff erence_type __n, __wrap_iter __x) _NOEXCEPT {
__x += __n;
return __x;
diff --git a/libcxx/test/libcxx/containers/sequences/array/nodiscard.iterator.verify.cpp b/libcxx/test/libcxx/containers/sequences/array/nodiscard.iterator.verify.cpp
new file mode 100644
index 0000000000000..a3ae77ae415f6
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/array/nodiscard.iterator.verify.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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();
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ *it;
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ it[0];
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ it + 1;
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ 1 + it;
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ it - 1;
+
+ // expected-warning-re at +1 {{{{(ignoring return value of function declared with 'nodiscard' attribute|expression result unused)}}}}
+ it - it;
+}
More information about the libcxx-commits
mailing list