[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `take_while_view` (PR #205172)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 22 12:55:25 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
Towards #<!-- -->172124
# References:
- https://wg21.link/range.take_while
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
---
Full diff: https://github.com/llvm/llvm-project/pull/205172.diff
2 Files Affected:
- (modified) libcxx/include/__ranges/take_while_view.h (+8-8)
- (added) libcxx/test/libcxx/ranges/range.adaptors/range.take_while/nodiscard.verify.cpp (+83)
``````````diff
diff --git a/libcxx/include/__ranges/take_while_view.h b/libcxx/include/__ranges/take_while_view.h
index 4977f139fc555..08e6a12c4bdbd 100644
--- a/libcxx/include/__ranges/take_while_view.h
+++ b/libcxx/include/__ranges/take_while_view.h
@@ -61,35 +61,35 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS take_while_view : public view_interfa
_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 take_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()
requires(!__simple_view<_View>)
{
return ranges::begin(__base_);
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>
{
return ranges::begin(__base_);
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
requires(!__simple_view<_View>)
{
return __sentinel</*_Const=*/false>(ranges::end(__base_), std::addressof(*__pred_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>
{
return __sentinel</*_Const=*/true>(ranges::end(__base_), std::addressof(*__pred_));
@@ -120,7 +120,7 @@ class take_while_view<_View, _Pred>::__sentinel {
requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
: __end_(std::move(__s.__end_)), __pred_(__s.__pred_) {}
- _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator_t<_Base>& __x, const __sentinel& __y) {
return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.take_while/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.take_while/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..e96fda9e903e9
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.take_while/nodiscard.verify.cpp
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "test_range.h"
+
+struct NonCommonView : std::ranges::view_base {
+ int* begin() const;
+ const int* end() const;
+
+ int* base();
+
+ int* begin();
+ const int* end();
+};
+static_assert(!std::ranges::common_range<NonCommonView>);
+
+struct NonSimpleView : std::ranges::view_base {
+ int* begin() const;
+ int* end() const;
+
+ const int* begin();
+ const int* end();
+
+ constexpr std::size_t size() { return 0; };
+};
+static_assert(!simple_view<NonSimpleView>);
+
+void test() {
+ NonCommonView range;
+ auto pred = [](int) { return true; };
+
+ auto nsv = std::views::take_while(NonSimpleView{}, pred);
+ auto v = std::views::take_while(range, pred);
+
+ // [range.take_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}}
+ nsv.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}}
+ nsv.end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).end();
+
+ // [range.take_while.sentinel]
+
+ auto st = v.end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st.base();
+
+ // [range.take_while.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::take_while(range, pred);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::take_while(pred);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/205172
More information about the libcxx-commits
mailing list