[libcxx-commits] [libcxx] ad850b8 - [libc++][ranges] Applied `[[nodiscard]]` to `split_view` (#205161)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jun 25 13:47:58 PDT 2026
Author: Hristo Hristov
Date: 2026-06-25T23:47:53+03:00
New Revision: ad850b8176cdb122ca1b8f0e1dce718e4f60f66d
URL: https://github.com/llvm/llvm-project/commit/ad850b8176cdb122ca1b8f0e1dce718e4f60f66d
DIFF: https://github.com/llvm/llvm-project/commit/ad850b8176cdb122ca1b8f0e1dce718e4f60f66d.diff
LOG: [libc++][ranges] Applied `[[nodiscard]]` to `split_view` (#205161)
Towards #172124
# References:
- https://wg21.link/range.split
-
https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
---------
Co-authored-by: Hristo Hristov <zingam at outlook.com>
Added:
libcxx/test/libcxx/ranges/range.adaptors/range.split/nodiscard.verify.cpp
Modified:
libcxx/include/__ranges/split_view.h
Removed:
################################################################################
diff --git a/libcxx/include/__ranges/split_view.h b/libcxx/include/__ranges/split_view.h
index 17a2ceeca6686..10148e0e64764 100644
--- a/libcxx/include/__ranges/split_view.h
+++ b/libcxx/include/__ranges/split_view.h
@@ -87,22 +87,22 @@ class split_view : public view_interface<split_view<_View, _Pattern>> {
_LIBCPP_HIDE_FROM_ABI constexpr explicit 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 {
@@ -141,9 +141,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..9a332e8309dd1
--- /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 <ranges>
+#include <utility>
+#include <vector>
+
+void test() {
+ 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);
+
+ // [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);
+}
More information about the libcxx-commits
mailing list