[libcxx-commits] [libcxx] [libc++] Remove [[nodiscard]] from map etc. operator[] (PR #172444)
Hans Wennborg via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 16 01:20:25 PST 2025
https://github.com/zmodem created https://github.com/llvm/llvm-project/pull/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.
>From 7c5c0aa73ae8070334ed7c74d8d5852c14bcd872 Mon Sep 17 00:00:00 2001
From: Hans Wennborg <hans at chromium.org>
Date: Tue, 16 Dec 2025 10:09:17 +0100
Subject: [PATCH] [libc++] Remove [[nodiscard]] from map etc. operator[]
---
libcxx/include/__flat_map/flat_map.h | 6 +++---
libcxx/include/map | 4 ++--
libcxx/include/unordered_map | 4 ++--
.../test/libcxx/diagnostics/flat_map.nodiscard.verify.cpp | 6 +++---
libcxx/test/libcxx/diagnostics/map.nodiscard.verify.cpp | 4 ++--
.../libcxx/diagnostics/unordered_map.nodiscard.verify.cpp | 4 ++--
6 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h
index 84b60cdc9ae27..4cd938b54cbf4 100644
--- a/libcxx/include/__flat_map/flat_map.h
+++ b/libcxx/include/__flat_map/flat_map.h
@@ -465,13 +465,13 @@ class flat_map {
}
// [flat.map.access], element access
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](const key_type& __x)
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](const key_type& __x)
requires is_constructible_v<mapped_type>
{
return try_emplace(__x).first->second;
}
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](key_type&& __x)
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](key_type&& __x)
requires is_constructible_v<mapped_type>
{
return try_emplace(std::move(__x)).first->second;
@@ -480,7 +480,7 @@ class flat_map {
template <class _Kp>
requires(__is_compare_transparent && is_constructible_v<key_type, _Kp> && is_constructible_v<mapped_type> &&
!is_convertible_v<_Kp &&, const_iterator> && !is_convertible_v<_Kp &&, iterator>)
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](_Kp&& __x) {
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](_Kp&& __x) {
return try_emplace(std::forward<_Kp>(__x)).first->second;
}
diff --git a/libcxx/include/map b/libcxx/include/map
index 6414c35a0799a..03c92e152e04f 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -1092,9 +1092,9 @@ public:
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __tree_.size(); }
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __tree_.max_size(); }
- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
# ifndef _LIBCPP_CXX03_LANG
- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
# endif
template <class _Arg,
diff --git a/libcxx/include/unordered_map b/libcxx/include/unordered_map
index 5df57b5cedb1f..ca53348eb5e2a 100644
--- a/libcxx/include/unordered_map
+++ b/libcxx/include/unordered_map
@@ -1262,9 +1262,9 @@ public:
}
# endif // _LIBCPP_STD_VER >= 20
- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
# ifndef _LIBCPP_CXX03_LANG
- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __k);
# endif
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __k);
diff --git a/libcxx/test/libcxx/diagnostics/flat_map.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/flat_map.nodiscard.verify.cpp
index 7d75083157aef..d2000b66147db 100644
--- a/libcxx/test/libcxx/diagnostics/flat_map.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/flat_map.nodiscard.verify.cpp
@@ -66,9 +66,9 @@ void test() {
TransparentKey<int> tkey;
std::flat_map<int, int> nfm;
- nfm[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- fm[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- fm[std::move(tkey)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ nfm[key]; // no-warning
+ fm[std::move(key)]; // no-warning
+ fm[std::move(tkey)]; // no-warning
fm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cfm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
diff --git a/libcxx/test/libcxx/diagnostics/map.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/map.nodiscard.verify.cpp
index ea110c35c03dd..20218b50e6c60 100644
--- a/libcxx/test/libcxx/diagnostics/map.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/map.nodiscard.verify.cpp
@@ -55,8 +55,8 @@ void test() {
int key = 0;
- m[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- m[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ m[key]; // no-warning
+ m[std::move(key)]; // no-warning
#if TEST_STD_VER >= 14
std::map<std::string, int, std::less<>> strMap;
diff --git a/libcxx/test/libcxx/diagnostics/unordered_map.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/unordered_map.nodiscard.verify.cpp
index 064b7a2fb444c..0747556905200 100644
--- a/libcxx/test/libcxx/diagnostics/unordered_map.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/unordered_map.nodiscard.verify.cpp
@@ -81,8 +81,8 @@ void test() {
ctm.equal_range(tkey); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif
- m[key]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- m[std::move(key)]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ m[key]; // no-warning
+ m[std::move(key)]; // no-warning
m.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
cm.at(key); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
More information about the libcxx-commits
mailing list