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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 9 03:12:57 PDT 2026


Author: Hristo Hristov
Date: 2026-06-09T13:12:53+03:00
New Revision: 09b451f1e7f7715b5cf994c24ef698b2a8579dbc

URL: https://github.com/llvm/llvm-project/commit/09b451f1e7f7715b5cf994c24ef698b2a8579dbc
DIFF: https://github.com/llvm/llvm-project/commit/09b451f1e7f7715b5cf994c24ef698b2a8579dbc.diff

LOG: [libc++][vector] Apply `[[nodiscard]]` to `vector<bool>::iterator` (#202265)

Towards #172124

Co-authored-by: Hristo Hristov <zingam at outlook.com>

Added: 
    libcxx/test/libcxx/containers/sequences/vector.bool/nodiscard.iterator.verify.cpp

Modified: 
    libcxx/include/__bit_reference

Removed: 
    


################################################################################
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+(
diff erence_type __n) const {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
+  operator+(
diff erence_type __n) const {
     __bit_iterator __t(*this);
     __t += __n;
     return __t;
   }
 
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(
diff erence_type __n) const {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator
+  operator-(
diff erence_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+(
diff erence_type __n, const __bit_iterator& __it) {
     return __it + __n;
   }
 
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend 
diff erence_type
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 friend 
diff erence_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[](
diff erence_type __n) const {
+  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference
+  operator[](
diff erence_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