Skip to content

Commit 01463e3

Browse files
H-G-Hristovaadeshps-mcw
authored andcommitted
[libc++][forward_list] Applied [[nodiscard]] (llvm#169019)
`[[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 d06af45 commit 01463e3

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

libcxx/include/forward_list

Lines changed: 15 additions & 13 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
}

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)