Skip to content

Commit b3bc005

Browse files
authored
[libc++][map] Applied [[nodiscard]] (#169971)
`[[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 - https://wg21.link/map
1 parent e45241a commit b3bc005

File tree

5 files changed

+173
-58
lines changed

5 files changed

+173
-58
lines changed

libcxx/include/__type_traits/is_generic_transparent_comparator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
_LIBCPP_BEGIN_NAMESPACE_STD
1919

20-
// This traits returns true if the given _Comparator is known to accept any two types for compaison. This is separate
20+
// This trait returns true if the given _Comparator is known to accept any two types for comparison. This is separate
2121
// from `__is_transparent_v`, since that only enables overloads of specific functions, but doesn't give any semantic
2222
// guarantees. This trait guarantess that the comparator simply calls the appropriate comparison functions for any two
2323
// types.

libcxx/include/map

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,33 +1069,37 @@ public:
10691069

10701070
_LIBCPP_HIDE_FROM_ABI ~map() { static_assert(sizeof(std::__diagnose_non_const_comparator<_Key, _Compare>()), ""); }
10711071

1072-
_LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __tree_.begin(); }
1073-
_LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __tree_.begin(); }
1074-
_LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __tree_.end(); }
1075-
_LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __tree_.end(); }
1072+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __tree_.begin(); }
1073+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __tree_.begin(); }
1074+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __tree_.end(); }
1075+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __tree_.end(); }
10761076

1077-
_LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
1078-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
1079-
_LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
1080-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
1077+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
1078+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
1079+
return const_reverse_iterator(end());
1080+
}
1081+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
1082+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
1083+
return const_reverse_iterator(begin());
1084+
}
10811085

1082-
_LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
1083-
_LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
1084-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
1085-
_LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
1086+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
1087+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
1088+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
1089+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
10861090

10871091
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __tree_.size() == 0; }
1088-
_LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
1089-
_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
1092+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
1093+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
10901094

1091-
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
1095+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
10921096
# ifndef _LIBCPP_CXX03_LANG
1093-
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
1097+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
10941098
# endif
10951099

10961100
template <class _Arg,
10971101
__enable_if_t<__is_transparently_comparable_v<_Compare, key_type, __remove_cvref_t<_Arg> >, int> = 0>
1098-
_LIBCPP_HIDE_FROM_ABI mapped_type& at(_Arg&& __arg) {
1102+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& at(_Arg&& __arg) {
10991103
auto [_, __child] = __tree_.__find_equal(__arg);
11001104
if (__child == nullptr)
11011105
std::__throw_out_of_range("map::at: key not found");
@@ -1104,19 +1108,23 @@ public:
11041108

11051109
template <class _Arg,
11061110
__enable_if_t<__is_transparently_comparable_v<_Compare, key_type, __remove_cvref_t<_Arg> >, int> = 0>
1107-
_LIBCPP_HIDE_FROM_ABI const mapped_type& at(_Arg&& __arg) const {
1111+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const mapped_type& at(_Arg&& __arg) const {
11081112
auto [_, __child] = __tree_.__find_equal(__arg);
11091113
if (__child == nullptr)
11101114
std::__throw_out_of_range("map::at: key not found");
11111115
return static_cast<__node_pointer>(__child)->__get_value().second;
11121116
}
11131117

1114-
_LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __k);
1115-
_LIBCPP_HIDE_FROM_ABI const mapped_type& at(const key_type& __k) const;
1118+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __k);
1119+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const mapped_type& at(const key_type& __k) const;
11161120

1117-
_LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT { return allocator_type(__tree_.__alloc()); }
1118-
_LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __tree_.value_comp().key_comp(); }
1119-
_LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__tree_.value_comp().key_comp()); }
1121+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
1122+
return allocator_type(__tree_.__alloc());
1123+
}
1124+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __tree_.value_comp().key_comp(); }
1125+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const {
1126+
return value_compare(__tree_.value_comp().key_comp());
1127+
}
11201128

11211129
# ifndef _LIBCPP_CXX03_LANG
11221130
template <class... _Args>
@@ -1268,10 +1276,10 @@ public:
12681276
"node_type with incompatible allocator passed to map::insert()");
12691277
return __tree_.template __node_handle_insert_unique<node_type>(__hint.__i_, std::move(__nh));
12701278
}
1271-
_LIBCPP_HIDE_FROM_ABI node_type extract(key_type const& __key) {
1279+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI node_type extract(key_type const& __key) {
12721280
return __tree_.template __node_handle_extract<node_type>(__key);
12731281
}
1274-
_LIBCPP_HIDE_FROM_ABI node_type extract(const_iterator __it) {
1282+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI node_type extract(const_iterator __it) {
12751283
return __tree_.template __node_handle_extract<node_type>(__it.__i_);
12761284
}
12771285
template <class _Compare2>
@@ -1302,44 +1310,48 @@ public:
13021310

13031311
_LIBCPP_HIDE_FROM_ABI void swap(map& __m) _NOEXCEPT_(__is_nothrow_swappable_v<__base>) { __tree_.swap(__m.__tree_); }
13041312

1305-
_LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
1306-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
1313+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __tree_.find(__k); }
1314+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __tree_.find(__k); }
13071315
# if _LIBCPP_STD_VER >= 14
13081316
template <typename _K2,
13091317
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13101318
int> = 0>
1311-
_LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
1319+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator find(const _K2& __k) {
13121320
return __tree_.find(__k);
13131321
}
13141322
template <typename _K2,
13151323
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13161324
int> = 0>
1317-
_LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
1325+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator find(const _K2& __k) const {
13181326
return __tree_.find(__k);
13191327
}
13201328
# endif
13211329

1322-
_LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __tree_.__count_unique(__k); }
1330+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const {
1331+
return __tree_.__count_unique(__k);
1332+
}
13231333
# if _LIBCPP_STD_VER >= 14
13241334
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
1325-
_LIBCPP_HIDE_FROM_ABI size_type count(const _K2& __k) const {
1335+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type count(const _K2& __k) const {
13261336
return __tree_.__count_multi(__k);
13271337
}
13281338
# endif
13291339

13301340
# if _LIBCPP_STD_VER >= 20
1331-
_LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __k) const { return find(__k) != end(); }
1341+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __k) const { return find(__k) != end(); }
13321342
template <typename _K2,
13331343
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13341344
int> = 0>
1335-
_LIBCPP_HIDE_FROM_ABI bool contains(const _K2& __k) const {
1345+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool contains(const _K2& __k) const {
13361346
return find(__k) != end();
13371347
}
13381348
# endif // _LIBCPP_STD_VER >= 20
13391349

1340-
_LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __k) { return __tree_.__lower_bound_unique(__k); }
1350+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __k) {
1351+
return __tree_.__lower_bound_unique(__k);
1352+
}
13411353

1342-
_LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __k) const {
1354+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __k) const {
13431355
return __tree_.__lower_bound_unique(__k);
13441356
}
13451357

@@ -1349,52 +1361,54 @@ public:
13491361
template <typename _K2,
13501362
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13511363
int> = 0>
1352-
_LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _K2& __k) {
1364+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _K2& __k) {
13531365
return __tree_.__lower_bound_multi(__k);
13541366
}
13551367

13561368
template <typename _K2,
13571369
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13581370
int> = 0>
1359-
_LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _K2& __k) const {
1371+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _K2& __k) const {
13601372
return __tree_.__lower_bound_multi(__k);
13611373
}
13621374
# endif
13631375

1364-
_LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __k) { return __tree_.__upper_bound_unique(__k); }
1376+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __k) {
1377+
return __tree_.__upper_bound_unique(__k);
1378+
}
13651379

1366-
_LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __k) const {
1380+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __k) const {
13671381
return __tree_.__upper_bound_unique(__k);
13681382
}
13691383

13701384
# if _LIBCPP_STD_VER >= 14
13711385
template <typename _K2,
13721386
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13731387
int> = 0>
1374-
_LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _K2& __k) {
1388+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _K2& __k) {
13751389
return __tree_.__upper_bound_multi(__k);
13761390
}
13771391
template <typename _K2,
13781392
enable_if_t<__is_transparent_v<_Compare, _K2> || __is_transparently_comparable_v<_Compare, key_type, _K2>,
13791393
int> = 0>
1380-
_LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _K2& __k) const {
1394+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _K2& __k) const {
13811395
return __tree_.__upper_bound_multi(__k);
13821396
}
13831397
# endif
13841398

1385-
_LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __k) {
1399+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __k) {
13861400
return __tree_.__equal_range_unique(__k);
13871401
}
1388-
_LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
1402+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
13891403
return __tree_.__equal_range_unique(__k);
13901404
}
13911405
# if _LIBCPP_STD_VER >= 14
13921406
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
1393-
_LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _K2& __k) {
1407+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _K2& __k) {
13941408
return __tree_.__equal_range_multi(__k);
13951409
}
13961410
template <typename _K2, enable_if_t<__is_transparent_v<_Compare, _K2>, int> = 0>
1397-
_LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _K2& __k) const {
1411+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const _K2& __k) const {
13981412
return __tree_.__equal_range_multi(__k);
13991413
}
14001414
# endif

libcxx/test/libcxx/containers/associative/map/at.abort.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ void exit_success(int) { std::_Exit(EXIT_SUCCESS); }
2828
int main(int, char**) {
2929
std::signal(SIGABRT, exit_success);
3030
std::map<int, int> map;
31-
map.at(1);
31+
(void)map.at(1);
3232
return EXIT_FAILURE;
3333
}

libcxx/test/libcxx/containers/associative/map/at.const.abort.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ void exit_success(int) { std::_Exit(EXIT_SUCCESS); }
2828
int main(int, char**) {
2929
std::signal(SIGABRT, exit_success);
3030
std::map<int, int> const map;
31-
map.at(1);
31+
(void)map.at(1);
3232
return EXIT_FAILURE;
3333
}

0 commit comments

Comments
 (0)