[libcxx-commits] [libcxx] ceea07d - [libc++][forward_list] Applied `[[nodiscard]]` (#169019)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 24 06:30:21 PST 2025
Author: Hristo Hristov
Date: 2025-11-24T16:30:17+02:00
New Revision: ceea07daa8a41562fdd884a224afbac1d7346e3e
URL: https://github.com/llvm/llvm-project/commit/ceea07daa8a41562fdd884a224afbac1d7346e3e
DIFF: https://github.com/llvm/llvm-project/commit/ceea07daa8a41562fdd884a224afbac1d7346e3e.diff
LOG: [libc++][forward_list] Applied `[[nodiscard]]` (#169019)
`[[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/forward_list
libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list
index 272e52d68f46a..56c45d0d46575 100644
--- a/libcxx/include/forward_list
+++ b/libcxx/include/forward_list
@@ -732,50 +732,52 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __v);
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT {
return allocator_type(this->__alloc_);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
return iterator(__base::__before_begin()->__next_);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
return const_iterator(__base::__before_begin()->__next_);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(nullptr); }
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
+ return iterator(nullptr);
+ }
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
return const_iterator(nullptr);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
return const_iterator(__base::__before_begin()->__next_);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
return const_iterator(nullptr);
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator before_begin() _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI iterator before_begin() _NOEXCEPT {
return iterator(__base::__before_begin());
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator before_begin() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator before_begin() const _NOEXCEPT {
return const_iterator(__base::__before_begin());
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbefore_begin() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_iterator cbefore_begin() const _NOEXCEPT {
return const_iterator(__base::__before_begin());
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
return __base::__before_begin()->__next_ == nullptr;
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
return std::min<size_type>(__node_traits::max_size(this->__alloc_), numeric_limits<
diff erence_type>::max());
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI reference front() {
_LIBCPP_ASSERT_NON_NULL(!empty(), "forward_list::front called on an empty list");
return __base::__before_begin()->__next_->__get_value();
}
- _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
+ [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX26 _LIBCPP_HIDE_FROM_ABI const_reference front() const {
_LIBCPP_ASSERT_NON_NULL(!empty(), "forward_list::front called on an empty list");
return __base::__before_begin()->__next_->__get_value();
}
diff --git a/libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp
index 7594a1d299a50..671c7f71ab2a2 100644
--- a/libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp
@@ -13,6 +13,27 @@
#include <forward_list>
void test() {
- std::forward_list<int> forward_list;
- forward_list.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::forward_list<int> fl;
+ const std::forward_list<int> cfl;
+
+ fl.get_allocator(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ fl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.end(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.cbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.cend(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.before_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.before_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.cbefore_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.cbefore_begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ fl.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ fl.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ fl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cfl.front(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
More information about the libcxx-commits
mailing list