Skip to content

Commit aee5e23

Browse files
committed
[libc++][forward_list] Applied [[nodiscard]]
`[[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
1 parent 13ed14f commit aee5e23

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

libcxx/include/forward_list

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -732,50 +732,52 @@ public:
732732

733733
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
734734

735-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
735+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
736736
return allocator_type(this->__alloc_);
737737
}
738738

739-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
739+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
740740
return iterator(__base::__before_begin()->__next_);
741741
}
742-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
742+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
743743
return const_iterator(__base::__before_begin()->__next_);
744744
}
745-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(nullptr); }
746-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
745+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
746+
return iterator(nullptr);
747+
}
748+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
747749
return const_iterator(nullptr);
748750
}
749751

750-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
752+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
751753
return const_iterator(__base::__before_begin()->__next_);
752754
}
753-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
755+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
754756
return const_iterator(nullptr);
755757
}
756758

757-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator before_begin() _NOEXCEPT {
759+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator before_begin() _NOEXCEPT {
758760
return iterator(__base::__before_begin());
759761
}
760-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator before_begin() const _NOEXCEPT {
762+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator before_begin() const _NOEXCEPT {
761763
return const_iterator(__base::__before_begin());
762764
}
763-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbefore_begin() const _NOEXCEPT {
765+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbefore_begin() const _NOEXCEPT {
764766
return const_iterator(__base::__before_begin());
765767
}
766768

767769
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
768770
return __base::__before_begin()->__next_ == nullptr;
769771
}
770-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
772+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
771773
return std::min<size_type>(__node_traits::max_size(this->__alloc_), numeric_limits<difference_type>::max());
772774
}
773775

774-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
776+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
775777
_LIBCPP_ASSERT_NON_NULL(!empty(), "forward_list::front called on an empty list");
776778
return __base::__before_begin()->__next_->__get_value();
777779
}
778-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
780+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
779781
_LIBCPP_ASSERT_NON_NULL(!empty(), "forward_list::front called on an empty list");
780782
return __base::__before_begin()->__next_->__get_value();
781783
}
@@ -822,15 +824,15 @@ public:
822824

823825
# if _LIBCPP_STD_VER >= 23
824826
template <_ContainerCompatibleRange<_Tp> _Range>
825-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator
826-
insert_range_after(const_iterator __position, _Range&& __range) {
827+
_LIBCPP_CONSTEXPR_SINCE_CXX26
828+
_LIBCPP_HIDE_FROM_ABI iterator insert_range_after(const_iterator __position, _Range&& __range) {
827829
return __insert_after_with_sentinel(__position, ranges::begin(__range), ranges::end(__range));
828830
}
829831
# endif
830832

831833
template <class _InputIterator, class _Sentinel>
832-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator
833-
__insert_after_with_sentinel(const_iterator __p, _InputIterator __f, _Sentinel __l);
834+
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI
835+
iterator __insert_after_with_sentinel(const_iterator __p, _InputIterator __f, _Sentinel __l);
834836

835837
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator erase_after(const_iterator __p);
836838
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator erase_after(const_iterator __f, const_iterator __l);
@@ -1516,8 +1518,8 @@ operator==(const forward_list<_Tp, _Alloc>& __x, const forward_list<_Tp, _Alloc>
15161518
# if _LIBCPP_STD_VER <= 17
15171519

15181520
template <class _Tp, class _Alloc>
1519-
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline _LIBCPP_HIDE_FROM_ABI bool
1520-
operator!=(const forward_list<_Tp, _Alloc>& __x, const forward_list<_Tp, _Alloc>& __y) {
1521+
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline
1522+
_LIBCPP_HIDE_FROM_ABI bool operator!=(const forward_list<_Tp, _Alloc>& __x, const forward_list<_Tp, _Alloc>& __y) {
15211523
return !(__x == __y);
15221524
}
15231525

@@ -1556,15 +1558,16 @@ operator<=>(const forward_list<_Tp, _Allocator>& __x, const forward_list<_Tp, _A
15561558
# endif // #if _LIBCPP_STD_VER <= 17
15571559

15581560
template <class _Tp, class _Alloc>
1559-
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline _LIBCPP_HIDE_FROM_ABI void
1560-
swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
1561+
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline
1562+
_LIBCPP_HIDE_FROM_ABI void swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y)
1563+
_NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
15611564
__x.swap(__y);
15621565
}
15631566

15641567
# if _LIBCPP_STD_VER >= 20
15651568
template <class _Tp, class _Allocator, class _Predicate>
1566-
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline _LIBCPP_HIDE_FROM_ABI typename forward_list<_Tp, _Allocator>::size_type
1567-
erase_if(forward_list<_Tp, _Allocator>& __c, _Predicate __pred) {
1569+
_LIBCPP_CONSTEXPR_SINCE_CXX26 inline _LIBCPP_HIDE_FROM_ABI
1570+
typename forward_list<_Tp, _Allocator>::size_type erase_if(forward_list<_Tp, _Allocator>& __c, _Predicate __pred) {
15681571
return __c.remove_if(__pred);
15691572
}
15701573

libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@
1313
#include <forward_list>
1414

1515
void test() {
16-
std::forward_list<int> forward_list;
17-
forward_list.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
16+
std::forward_list<int> fl;
17+
const std::forward_list<int> cfl;
18+
19+
fl.get_allocator(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
21+
fl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
cfl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
23+
fl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
24+
cfl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
fl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
26+
cfl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
fl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
cfl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
fl.before_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
cfl.before_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
fl.cbefore_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
cfl.cbefore_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
34+
fl.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
fl.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
36+
37+
fl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
38+
cfl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
1839
}

0 commit comments

Comments
 (0)