[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 04:44:59 PST 2025
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/173557
`[[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
>From 7c62f87896a911332a416c1aa49820cb4acaf112 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 | 45 +++++++++++++++++++
2 files changed, 53 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..214536ece1065
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.drop/nodiscard.verify.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <vector>
+#include <utility>
+
+void test() {
+ std::vector<int> range;
+
+ auto v = std::views::drop(range);
+
+ // [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]
+}
More information about the libcxx-commits
mailing list