[libc++][list] Applied [[nodiscard]] (#169015)

`[[nodiscard]]` should be applied to functions where discarding the
return value is most likely a correctness issue.
-
https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
This commit is contained in:
Hristo Hristov 2025-11-24 12:32:50 +02:00 committed by GitHub
parent 8c6ec12127
commit 4c4cf71095
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 21 deletions

View File

@ -774,57 +774,71 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return this->__size_; }
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
return this->__size_;
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
return __base::empty();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
return std::min<size_type>(this->__node_alloc_max_size(), numeric_limits<difference_type >::max());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __base::begin(); }
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __base::begin(); }
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __base::end(); }
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __base::end(); }
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
return __base::begin();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return __base::end(); }
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
return __base::begin();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
return __base::end();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
return __base::end();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
return __base::begin();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
return __base::end();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
return reverse_iterator(end());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
rbegin() const _NOEXCEPT {
return const_reverse_iterator(end());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
return reverse_iterator(begin());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
return const_reverse_iterator(begin());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
crbegin() const _NOEXCEPT {
return const_reverse_iterator(end());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
return const_reverse_iterator(begin());
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
return __base::__end_.__next_->__as_node()->__get_value();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
return __base::__end_.__next_->__as_node()->__get_value();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference back() {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference back() {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
return __base::__end_.__prev_->__as_node()->__get_value();
}
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference back() const {
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference back() const {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
return __base::__end_.__prev_->__as_node()->__get_value();
}

View File

@ -13,6 +13,33 @@
#include <list>
void test() {
std::list<int> list;
list.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::list<int> l;
const std::list<int> cl;
l.get_allocator(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.crend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.crend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
l.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cl.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}