Skip to content

Commit 952bff5

Browse files
committed
[libc++][deque] 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 d5778a7 commit 952bff5

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

libcxx/include/deque

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -715,45 +715,53 @@ public:
715715

716716
// iterators:
717717

718-
_LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
718+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
719719
__map_pointer __mp = __map_.begin() + __start_ / __block_size;
720720
return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
721721
}
722722

723-
_LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
723+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
724724
__map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
725725
return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
726726
}
727727

728-
_LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
728+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
729729
size_type __p = size() + __start_;
730730
__map_pointer __mp = __map_.begin() + __p / __block_size;
731731
return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
732732
}
733733

734-
_LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
734+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
735735
size_type __p = size() + __start_;
736736
__map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
737737
return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
738738
}
739739

740-
_LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
741-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
742-
_LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
743-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
740+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
741+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
742+
return const_reverse_iterator(end());
743+
}
744+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
745+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
746+
return const_reverse_iterator(begin());
747+
}
744748

745-
_LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
746-
_LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
747-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
748-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
749+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
750+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
751+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
752+
return const_reverse_iterator(end());
753+
}
754+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
755+
return const_reverse_iterator(begin());
756+
}
749757

750758
// capacity:
751-
_LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
759+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
752760

753761
_LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_; }
754762
_LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_; }
755763

756-
_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
764+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
757765
return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<difference_type>::max());
758766
}
759767
_LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
@@ -762,14 +770,14 @@ public:
762770
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
763771

764772
// element access:
765-
_LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
766-
_LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
767-
_LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
768-
_LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
769-
_LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
770-
_LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
771-
_LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
772-
_LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
773+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
774+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
775+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
776+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
777+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
778+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
779+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
780+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
773781

774782
// 23.2.2.3 modifiers:
775783
_LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);

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

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

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

0 commit comments

Comments
 (0)