Skip to content

Commit c3ebcfe

Browse files
authored
[libc++] Remove [[nodiscard]] from map etc. operator[] (#172444)
This was added in #169971 and related PRs. As commented there, discarding the return value on operator[] for these types does not typically indicate a bug since the operator can also be used to insert a default value.
1 parent a96ce5f commit c3ebcfe

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

libcxx/include/__flat_map/flat_map.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,13 @@ class flat_map {
465465
}
466466

467467
// [flat.map.access], element access
468-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](const key_type& __x)
468+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](const key_type& __x)
469469
requires is_constructible_v<mapped_type>
470470
{
471471
return try_emplace(__x).first->second;
472472
}
473473

474-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](key_type&& __x)
474+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](key_type&& __x)
475475
requires is_constructible_v<mapped_type>
476476
{
477477
return try_emplace(std::move(__x)).first->second;
@@ -480,7 +480,7 @@ class flat_map {
480480
template <class _Kp>
481481
requires(__is_compare_transparent && is_constructible_v<key_type, _Kp> && is_constructible_v<mapped_type> &&
482482
!is_convertible_v<_Kp &&, const_iterator> && !is_convertible_v<_Kp &&, iterator>)
483-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](_Kp&& __x) {
483+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](_Kp&& __x) {
484484
return try_emplace(std::forward<_Kp>(__x)).first->second;
485485
}
486486

libcxx/include/map

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,9 @@ public:
10921092
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
10931093
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
10941094

1095-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
1095+
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
10961096
# ifndef _LIBCPP_CXX03_LANG
1097-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
1097+
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
10981098
# endif
10991099

11001100
template <class _Arg,

libcxx/include/unordered_map

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,9 +1262,9 @@ public:
12621262
}
12631263
# endif // _LIBCPP_STD_VER >= 20
12641264

1265-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
1265+
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
12661266
# ifndef _LIBCPP_CXX03_LANG
1267-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
1267+
_LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
12681268
# endif
12691269

12701270
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __k);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ void test() {
6666
TransparentKey<int> tkey;
6767

6868
std::flat_map<int, int> nfm;
69-
nfm[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
70-
fm[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
71-
fm[std::move(tkey)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
69+
nfm[key]; // no-warning
70+
fm[std::move(key)]; // no-warning
71+
fm[std::move(tkey)]; // no-warning
7272

7373
fm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
7474
cfm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void test() {
5555

5656
int key = 0;
5757

58-
m[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
59-
m[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
58+
m[key]; // no-warning
59+
m[std::move(key)]; // no-warning
6060

6161
#if TEST_STD_VER >= 14
6262
std::map<std::string, int, std::less<>> strMap;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ void test() {
8181
ctm.equal_range(tkey); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
8282
#endif
8383

84-
m[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
85-
m[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
84+
m[key]; // no-warning
85+
m[std::move(key)]; // no-warning
8686

8787
m.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
8888
cm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

0 commit comments

Comments
 (0)