[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `chunk_by_view` (PR #173211)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Dec 21 20:24:50 PST 2025
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/173211
`[[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/ranges
- https://wg21.link/range.drop.while
Towards #172124
>From e1fbaa077b6832164226ea476470d7bb11112ab2 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Dec 2025 06:23:56 +0200
Subject: [PATCH] [libc++][ranges] Applied `[[nodiscard]]` to `chunk_by_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/ranges
- https://wg21.link/range.drop.while
Towards #172124
---
libcxx/include/__ranges/drop_while_view.h | 10 ++--
.../range.drop.while/nodiscard.verify.cpp | 46 +++++++++++++++++++
2 files changed, 51 insertions(+), 5 deletions(-)
create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.drop.while/nodiscard.verify.cpp
diff --git a/libcxx/include/__ranges/drop_while_view.h b/libcxx/include/__ranges/drop_while_view.h
index bc7f019393a80..1fe4e17f8048b 100644
--- a/libcxx/include/__ranges/drop_while_view.h
+++ b/libcxx/include/__ranges/drop_while_view.h
@@ -57,17 +57,17 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS drop_while_view : public view_interfa
_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 drop_while_view(_View __base, _Pred __pred)
: __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {}
- _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 const _Pred& pred() const { return *__pred_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
// Note: this duplicates a check in `optional` but provides a better error message.
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
__pred_.__has_value(),
@@ -83,7 +83,7 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS drop_while_view : public view_interfa
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return ranges::end(__base_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return ranges::end(__base_); }
private:
_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.drop.while/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.drop.while/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..039e3ddffa312
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.drop.while/nodiscard.verify.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 pred = [](int) { return true; };
+
+ auto v = std::views::drop_while(range, pred);
+
+ // [range.drop.while.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.drop.while.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::drop_while(range, pred);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::drop_while(range);
+}
More information about the libcxx-commits
mailing list