[libc++][ranges] Applied [[nodiscard]] to take_while_view - #205172
Merged
Zingam merged 2 commits intoJun 25, 2026
Conversation
Zingam
marked this pull request as ready for review
June 22, 2026 19:54
|
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) ChangesTowards #172124 References:
Full diff: https://github.com/llvm/llvm-project/pull/205172.diff 2 Files Affected:
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@+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.pred();
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ nsv.begin();
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).begin();
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ nsv.end();
+
+ // expected-warning@+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@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st.base();
+
+ // [range.take_while.overview]
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::take_while(range, pred);
+
+ // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::take_while(pred);
+}
|
frederick-vs-ja
approved these changes
Jun 25, 2026
maarcosrmz
pushed a commit
to maarcosrmz/llvm-project
that referenced
this pull request
Jul 1, 2026
…05172) Towards llvm#172124 # References: - https://wg21.link/range.take_while - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant Co-authored-by: Hristo Hristov <zingam@outlook.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Towards #172124
References: