[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `filter_view` (PR #173460)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 23 22:30:43 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/range.filter
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/173460.diff
2 Files Affected:
- (modified) libcxx/include/__ranges/filter_view.h (+10-10)
- (added) libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp (+75)
``````````diff
diff --git a/libcxx/include/__ranges/filter_view.h b/libcxx/include/__ranges/filter_view.h
index 07980e7353190..3ad69ea100931 100644
--- a/libcxx/include/__ranges/filter_view.h
+++ b/libcxx/include/__ranges/filter_view.h
@@ -76,16 +76,16 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS filter_view : public view_interface<f
: __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {}
template <class _Vp = _View>
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
requires copy_constructible<_Vp>
{
return __base_;
}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
- _LIBCPP_HIDE_FROM_ABI constexpr _Pred const& pred() const { return *__pred_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Pred const& pred() const { return *__pred_; }
- _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {
// Note: this duplicates a check in `optional` but provides a better error message.
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__pred_.__has_value(), "Trying to call begin() on a filter_view that does not have a valid predicate.");
@@ -99,7 +99,7 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS filter_view : public view_interface<f
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
if constexpr (common_range<_View>)
return __iterator{*this, ranges::end(__base_)};
else
@@ -148,10 +148,10 @@ class filter_view<_View, _Pred>::__iterator : public __filter_iterator_category<
_LIBCPP_HIDE_FROM_ABI constexpr __iterator(filter_view& __parent, iterator_t<_View> __current)
: __current_(std::move(__current)), __parent_(std::addressof(__parent)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> const& base() const& noexcept { return __current_; }
- _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() && { return std::move(__current_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> const& base() const& noexcept { return __current_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() && { return std::move(__current_); }
- _LIBCPP_HIDE_FROM_ABI constexpr range_reference_t<_View> operator*() const { return *__current_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_reference_t<_View> operator*() const { return *__current_; }
_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> operator->() const
requires __has_arrow<iterator_t<_View>> && copyable<iterator_t<_View>>
{
@@ -194,7 +194,7 @@ class filter_view<_View, _Pred>::__iterator : public __filter_iterator_category<
return __x.__current_ == __y.__current_;
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_rvalue_reference_t<_View>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_rvalue_reference_t<_View>
iter_move(__iterator const& __it) noexcept(noexcept(ranges::iter_move(__it.__current_))) {
return ranges::iter_move(__it.__current_);
}
@@ -218,7 +218,7 @@ class filter_view<_View, _Pred>::__sentinel {
_LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(filter_view& __parent) : __end_(ranges::end(__parent.__base_)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_View> base() const { return __end_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_View> base() const { return __end_; }
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(__iterator const& __x, __sentinel const& __y) {
return __x.__current_ == __y.__end_;
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..a4b3cbb3b9210
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++20
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <utility>
+
+struct NonCommonView : std::ranges::view_base {
+ int* begin() const;
+ const int* end() const;
+
+ int* base();
+
+ int* begin();
+ const int* end();
+};
+static_assert(!std::ranges::common_range<NonCommonView>);
+
+void test() {
+ NonCommonView range;
+ auto pred = [](int) { return true; };
+
+ auto v = std::views::filter(range, pred);
+
+ // [range.filter.view]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(v).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.pred();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.end();
+
+ // [range.filter.iterator]
+
+ auto it = v.begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it.base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(it).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *it;
+
+ // [range.filter.sentinel]
+
+ auto st = v.end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st.base();
+
+ // [range.filter.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::filter(range, pred);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::filter(pred);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/173460
More information about the libcxx-commits
mailing list