[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `filter_view` (PR #173460)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 25 01:11:28 PST 2025


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/173460

>From 4ef233d3c9ea67015c163d4889fe38131e61d301 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Wed, 24 Dec 2025 08:26:04 +0200
Subject: [PATCH 1/2] [libc++][ranges] Applied `[[nodiscard]]` to `filter_view`

`[[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
---
 libcxx/include/__ranges/filter_view.h         | 20 ++---
 .../range.filter/nodiscard.verify.cpp         | 75 +++++++++++++++++++
 2 files changed, 85 insertions(+), 10 deletions(-)
 create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp

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);
+}

>From 964e0fdf38e38ec1cae4120938ba488fb0cb196f Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Thu, 25 Dec 2025 11:11:12 +0200
Subject: [PATCH 2/2] Addressed review comments

---
 .../ranges/range.adaptors/range.filter/nodiscard.verify.cpp    | 3 +++
 1 file changed, 3 insertions(+)

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
index a4b3cbb3b9210..0aaf9aec6cc29 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.filter/nodiscard.verify.cpp
@@ -55,6 +55,9 @@ void test() {
   // 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}}
+  iter_move(it);
+
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   *it;
 



More information about the libcxx-commits mailing list