Skip to content

Commit 564df03

Browse files
H-G-Hristovaadeshps-mcw
authored andcommitted
[libc++][list] Applied [[nodiscard]] (llvm#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
1 parent f008f19 commit 564df03

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

libcxx/include/list

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -774,57 +774,71 @@ public:
774774

775775
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x);
776776

777-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
777+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT;
778778

779-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return this->__size_; }
779+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
780+
return this->__size_;
781+
}
780782
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
781783
return __base::empty();
782784
}
783-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
785+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
784786
return std::min<size_type>(this->__node_alloc_max_size(), numeric_limits<difference_type >::max());
785787
}
786788

787-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __base::begin(); }
788-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __base::begin(); }
789-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __base::end(); }
790-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __base::end(); }
791-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
789+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
790+
return __base::begin();
791+
}
792+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
793+
return __base::begin();
794+
}
795+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
796+
return __base::end();
797+
}
798+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
799+
return __base::end();
800+
}
801+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
792802
return __base::begin();
793803
}
794-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return __base::end(); }
804+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
805+
return __base::end();
806+
}
795807

796-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
808+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
797809
return reverse_iterator(end());
798810
}
799-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
811+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
812+
rbegin() const _NOEXCEPT {
800813
return const_reverse_iterator(end());
801814
}
802-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
815+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
803816
return reverse_iterator(begin());
804817
}
805-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
818+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
806819
return const_reverse_iterator(begin());
807820
}
808-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
821+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
822+
crbegin() const _NOEXCEPT {
809823
return const_reverse_iterator(end());
810824
}
811-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
825+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
812826
return const_reverse_iterator(begin());
813827
}
814828

815-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
829+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
816830
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
817831
return __base::__end_.__next_->__as_node()->__get_value();
818832
}
819-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
833+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
820834
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list");
821835
return __base::__end_.__next_->__as_node()->__get_value();
822836
}
823-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference back() {
837+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference back() {
824838
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
825839
return __base::__end_.__prev_->__as_node()->__get_value();
826840
}
827-
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference back() const {
841+
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference back() const {
828842
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list");
829843
return __base::__end_.__prev_->__as_node()->__get_value();
830844
}

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

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

1515
void test() {
16-
std::list<int> list;
17-
list.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
16+
std::list<int> l;
17+
const std::list<int> cl;
18+
19+
l.get_allocator(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
20+
l.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
l.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
22+
l.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
23+
24+
l.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
25+
cl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
26+
l.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
27+
cl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
l.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
cl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
l.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
cl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
l.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
cl.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
34+
l.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
cl.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
36+
l.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
cl.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
38+
l.crend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
cl.crend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
40+
41+
l.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
cl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
43+
l.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
cl.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
1845
}

0 commit comments

Comments
 (0)