[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `split_view` (PR #205161)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 22 12:53:38 PDT 2026
https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/205161
>From 37d274c134b0b3e07e5593723fc05d85d206cc56 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Jun 2026 21:31:17 +0300
Subject: [PATCH 1/4] [libc++][ranges] Applied `[[nodiscard]]` to `split_view`
Towards #172124
References:
https://wg21.link/range.split
https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
---
libcxx/include/__ranges/split_view.h | 12 ++---
.../range.split/nodiscard.verify.cpp | 53 +++++++++++++++++++
2 files changed, 59 insertions(+), 6 deletions(-)
create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
diff --git a/libcxx/include/__ranges/split_view.h b/libcxx/include/__ranges/split_view.h
index 2ec908ba4070e..2e476691d353f 100644
--- a/libcxx/include/__ranges/split_view.h
+++ b/libcxx/include/__ranges/split_view.h
@@ -88,22 +88,22 @@ class split_view : public view_interface<split_view<_View, _Pattern>> {
split_view(_Range&& __range, range_value_t<_Range> __elem)
: __base_(views::all(std::forward<_Range>(__range))), __pattern_(views::single(std::move(__elem))) {}
- _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 __iterator begin() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {
if (!__cached_begin_.__has_value()) {
__cached_begin_.__emplace(__find_next(ranges::begin(__base_)));
}
return {*this, ranges::begin(__base_), *__cached_begin_};
}
- _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 {
@@ -142,9 +142,9 @@ struct split_view<_View, _Pattern>::__iterator {
split_view<_View, _Pattern>& __parent, iterator_t<_View> __current, subrange<iterator_t<_View>> __next)
: __parent_(std::addressof(__parent)), __cur_(std::move(__current)), __next_(std::move(__next)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() const { return __cur_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() const { return __cur_; }
- _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return {__cur_, __next_.begin()}; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return {__cur_, __next_.begin()}; }
_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
__cur_ = __next_.begin();
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..213d88c834a53
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <string>
+#include <ranges>
+#include <utility>
+
+void test() {
+ std::string range{"19,28,29,49,82,94"};
+ char pattern = ',';
+
+ auto v = std::views::split(range, pattern);
+
+ // [range.split.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}}
+ v.end();
+
+ // [range.split.iterator]
+
+ auto it = v.begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(it).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *std::as_const(it);
+
+ // [range.split.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::split(range, pattern);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::split(pattern);
+}
>From 664355fa998cb0ae4fe6c9b65dc29ae1d1711ec4 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Jun 2026 22:46:31 +0300
Subject: [PATCH 2/4] Updated
---
.../ranges/range.adaptors/range.split/nodiscard.verify.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
index 213d88c834a53..ec07f89e95fcf 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
@@ -10,12 +10,12 @@
// Check that functions are marked [[nodiscard]]
-#include <string>
#include <ranges>
#include <utility>
+#include <vector>
void test() {
- std::string range{"19,28,29,49,82,94"};
+ std::vector<char> range = {'1', '9', ',', '2', '8', ',', '2', '9', ',', '4', '9', ',', '8', '2', ',', '9', '4'};
char pattern = ',';
auto v = std::views::split(range, pattern);
>From 19b13074c0d2edf3799f7ade77c970e517f14e89 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Jun 2026 22:52:04 +0300
Subject: [PATCH 3/4] Fix formatting
---
.../ranges/range.adaptors/range.split/nodiscard.verify.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
index ec07f89e95fcf..a68f7f1b71aaf 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
@@ -16,7 +16,7 @@
void test() {
std::vector<char> range = {'1', '9', ',', '2', '8', ',', '2', '9', ',', '4', '9', ',', '8', '2', ',', '9', '4'};
- char pattern = ',';
+ char pattern = ',';
auto v = std::views::split(range, pattern);
>From cafa2edaa664c46deffaf682c32d38d04f3a0d33 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Jun 2026 22:53:13 +0300
Subject: [PATCH 4/4] Fix
---
.../ranges/range.adaptors/range.split/nodiscard.verify.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
index a68f7f1b71aaf..9a332e8309dd1 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
@@ -15,7 +15,7 @@
#include <vector>
void test() {
- std::vector<char> range = {'1', '9', ',', '2', '8', ',', '2', '9', ',', '4', '9', ',', '8', '2', ',', '9', '4'};
+ std::vector<char> range = {'1', '9', ' ', '2', '8', ' ', '2', '9', ',', '4', '9', ' ', '8', '2', ' ', '9', '4'};
char pattern = ',';
auto v = std::views::split(range, pattern);
More information about the libcxx-commits
mailing list