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

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


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

>From bfa0ede0db3e9e43863d8c7a2fdf8a2efc8ff192 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Thu, 25 Dec 2025 14:44:25 +0200
Subject: [PATCH] [libc+][ranges] Applied `[[nodiscard]]` to `drop_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.drop

Towards #172124
---
 libcxx/include/__ranges/drop_view.h           |  16 +--
 .../range.drop/nodiscard.verify.cpp           | 102 ++++++++++++++++++
 2 files changed, 110 insertions(+), 8 deletions(-)
 create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.drop/nodiscard.verify.cpp

diff --git a/libcxx/include/__ranges/drop_view.h b/libcxx/include/__ranges/drop_view.h
index 42ada9299a779..feb3705d2df6c 100644
--- a/libcxx/include/__ranges/drop_view.h
+++ b/libcxx/include/__ranges/drop_view.h
@@ -80,14 +80,14 @@ class drop_view : public view_interface<drop_view<_View>> {
     _LIBCPP_ASSERT_UNCATEGORIZED(__count_ >= 0, "count must be greater than or equal to zero.");
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
     requires copy_constructible<_View>
   {
     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 auto begin()
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
     requires(!(__simple_view<_View> && random_access_range<const _View> && sized_range<const _View>))
   {
     if constexpr (random_access_range<_View> && sized_range<_View>) {
@@ -104,20 +104,20 @@ class drop_view : public view_interface<drop_view<_View>> {
     return __tmp;
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
     requires random_access_range<const _View> && sized_range<const _View>
   {
     const auto __dist = std::min(ranges::distance(__base_), __count_);
     return ranges::begin(__base_) + __dist;
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
     requires(!__simple_view<_View>)
   {
     return ranges::end(__base_);
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
     requires range<const _View>
   {
     return ranges::end(__base_);
@@ -129,13 +129,13 @@ class drop_view : public view_interface<drop_view<_View>> {
     return __s < __c ? 0 : __s - __c;
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
     requires sized_range<_View>
   {
     return __size(*this);
   }
 
-  _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
     requires sized_range<const _View>
   {
     return __size(*this);
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.drop/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.drop/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..1a98f8a8d5797
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.drop/nodiscard.verify.cpp
@@ -0,0 +1,102 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <span>
+#include <vector>
+#include <utility>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+void test() {
+  std::vector<int> range;
+
+  auto v = std::views::drop(range, 0);
+
+  // [range.drop.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.begin();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).begin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.end();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).end();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.size();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).size();
+
+  // [range.drop.overview]
+
+  { // The `empty_view` case.
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::views::empty<int> | std::views::drop(0);
+  }
+
+  { // The `span | basic_string_view | iota_view | subrange (StoreSize == false)` case.
+    int arr[]{94, 82, 49};
+    auto subrange = std::ranges::subrange(arr, arr + 3);
+    LIBCPP_STATIC_ASSERT(!decltype(subrange)::_StoreSize);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    subrange | std::views::drop(3);
+  }
+
+  { // The the `subrange (StoreSize == true)` case.
+    struct SizedViewWithUnsizedSentinel : std::ranges::view_base {
+      using iterator = random_access_iterator<int*>;
+      using sentinel = sentinel_wrapper<random_access_iterator<int*>>;
+
+      int* begin_ = nullptr;
+      int* end_   = nullptr;
+      constexpr SizedViewWithUnsizedSentinel(int* begin, int* end) : begin_(begin), end_(end) {}
+
+      constexpr auto begin() const { return iterator(begin_); }
+      constexpr auto end() const { return sentinel(iterator(end_)); }
+      constexpr std::size_t size() const { return end_ - begin_; }
+    };
+
+    int arr[]{94, 82, 49};
+
+    using View = SizedViewWithUnsizedSentinel;
+    View view(arr, arr + 3);
+
+    using Subrange = std::ranges::subrange<View::iterator, View::sentinel, std::ranges::subrange_kind::sized>;
+    auto subrange  = Subrange(view.begin(), view.end(), std::ranges::distance(view.begin(), view.end()));
+    LIBCPP_STATIC_ASSERT(decltype(subrange)::_StoreSize);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    subrange | std::views::drop(3);
+  }
+
+#if TEST_STD_VER >= 23
+  { // The `repeat_view` "_RawRange models sized_range" case.
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::repeat_view<int, int>(1, 8) | std::views::drop(3);
+  }
+
+  { // The `repeat_view` "otherwise" case.
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::repeat_view<int>(1) | std::views::drop(3);
+  }
+#endif
+}



More information about the libcxx-commits mailing list