[libcxx-commits] [libcxx] d6be9fc - [libc++][deque] Applied `[[nodiscard]]` (#169745)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 27 03:53:39 PST 2025
Author: Hristo Hristov
Date: 2025-11-27T19:53:34+08:00
New Revision: d6be9fc115459ce154f8aa062b05645adb150469
URL: https://github.com/llvm/llvm-project/commit/d6be9fc115459ce154f8aa062b05645adb150469
DIFF: https://github.com/llvm/llvm-project/commit/d6be9fc115459ce154f8aa062b05645adb150469.diff
LOG: [libc++][deque] Applied `[[nodiscard]]` (#169745)
`[[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
Added:
Modified:
libcxx/include/deque
libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/deque b/libcxx/include/deque
index 08bf8141eb782..ad2d759e1fcac 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -715,45 +715,53 @@ public:
// iterators:
- _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
__map_pointer __mp = __map_.begin() + __start_ / __block_size;
return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
}
- _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
__map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
}
- _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
size_type __p = size() + __start_;
__map_pointer __mp = __map_.begin() + __p / __block_size;
return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
}
- _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
size_type __p = size() + __start_;
__map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
}
- _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
- _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
- _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
- _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
+ return const_reverse_iterator(end());
+ }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
+ return const_reverse_iterator(begin());
+ }
- _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
- _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
- _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
- _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
+ return const_reverse_iterator(end());
+ }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
+ return const_reverse_iterator(begin());
+ }
// capacity:
- _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size(); }
_LIBCPP_HIDE_FROM_ABI size_type& __size() _NOEXCEPT { return __size_; }
_LIBCPP_HIDE_FROM_ABI const size_type& __size() const _NOEXCEPT { return __size_; }
- _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
return std::min<size_type>(__alloc_traits::max_size(__alloc()), numeric_limits<
diff erence_type>::max());
}
_LIBCPP_HIDE_FROM_ABI void resize(size_type __n);
@@ -762,14 +770,14 @@ public:
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
// element access:
- _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
- _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
- _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __i) _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __i) const _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference at(size_type __i);
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __i) const;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT;
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT;
// 23.2.2.3 modifiers:
_LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __v);
diff --git a/libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp
index e8dda09567613..a9adb1757b8ef 100644
--- a/libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp
@@ -13,6 +13,32 @@
#include <deque>
void test() {
- std::deque<int> deque;
- deque.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::deque<int> d;
+ const std::deque<int> cd;
+
+ d.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.rbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.rend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.crend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ d.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ d[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd[0]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.at(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.at(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cd.back(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
More information about the libcxx-commits
mailing list