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

Towards #172124

Co-authored-by: Hristo Hristov <zingam@outlook.com>
This commit is contained in:
Hristo HristovandHristo Hristov authored and GitHub committed 2026-06-09 13:12:53 +03:00
1 parent 96a263602f
commit 09b451f1e7
2 files changed
+61 -6

No files matched your search

+9 -6
View File
@@ -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);
}
@@ -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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
*it;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
*cit;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
it + 1;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
cit + 1;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
it - 1;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
cit - 1;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
1 + it;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
1 + cit;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
it - it;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
cit - cit;
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
it[0];
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
cit[0];
}