[libcxx-commits] [libcxx] [libc++][vector] Apply `[[nodiscard]]` to `vector<bool>::iterator` (PR #202265)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 7 23:33:06 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>

Towards #<!-- -->172124

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


2 Files Affected:

- (modified) libcxx/include/__bit_reference (+6-6) 
- (added) libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp (+52) 


``````````diff
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index a4fd6602770ef..3b3ff67de5c6a 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,29 @@ 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];
+}

``````````

</details>


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


More information about the libcxx-commits mailing list