[libcxx-commits] [libcxx] [libc++][vector] Apply `[[nodiscard]]` to `vector<bool>::iterator` (PR #202265)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 7 23:34:41 PDT 2026
https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/202265
>From 45451be5428878f12a2781fb7ccc5410b36c5724 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 8 Jun 2026 09:21:13 +0300
Subject: [PATCH] [libc++][vector] Apply `[[nodiscard]]` to
`vector<bool>::iterator`
Towards #172124
---
libcxx/include/__bit_reference | 15 +++---
.../vector.bool/nodiscard.iterator.verify.cpp | 52 +++++++++++++++++++
2 files changed, 61 insertions(+), 6 deletions(-)
create mode 100644 libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index a4fd6602770ef..692686f1584d0 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -336,7 +336,7 @@ public:
}
#endif
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
_LIBCPP_ASSERT_INTERNAL(__ctz_ < __bits_per_word, "Dereferencing an invalid __bit_iterator.");
return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >(
__seg_, __storage_type(1) << __ctz_);
@@ -389,29 +389,32 @@ public:
return *this += -__n;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator+(difference_type __n) const {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
+ operator+(difference_type __n) const {
__bit_iterator __t(*this);
__t += __n;
return __t;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(difference_type __n) const {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
+ operator-(difference_type __n) const {
__bit_iterator __t(*this);
__t -= __n;
return __t;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator
operator+(difference_type __n, const __bit_iterator& __it) {
return __it + __n;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend difference_type
operator-(const __bit_iterator& __x, const __bit_iterator& __y) {
return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;
}
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](difference_type __n) const {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference
+ operator[](difference_type __n) const {
return *(*this + __n);
}
diff --git a/libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp b/libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp
new file mode 100644
index 0000000000000..6d8e652d0d8a2
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <vector>
+
+#include "test_macros.h"
+
+void test() {
+ typedef std::vector<bool> Container;
+ Container c;
+ Container::iterator it = c.begin();
+ Container::const_iterator cit = c.cbegin();
+
+ // 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}}
+ *cit;
+
+ // 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}}
+ cit + 1;
+
+ // 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}}
+ cit - 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}}
+ 1 + cit;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - it;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cit - cit;
+
+ // 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}}
+ cit[0];
+}
More information about the libcxx-commits
mailing list