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> { _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> __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 +#include +#include + +void test() { + std::vector 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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + v.base(); + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + std::move(v).base(); + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + v.begin(); + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + v.end(); + + // [range.split.iterator] + + auto it = v.begin(); + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + std::as_const(it).base(); + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + *std::as_const(it); + + // [range.split.overview] + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + std::views::split(range, pattern); + + // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} + std::views::split(pattern); +}