Skip to content

[libc++][ranges] Applied [[nodiscard]] to take_while_view - #205172

Merged
Zingam merged 2 commits into
llvm:mainfrom
H-G-Hristov:hgh/libcxx/nodiscard-to-take_while
Jun 25, 2026
Merged

[libc++][ranges] Applied [[nodiscard]] to take_while_view#205172
Zingam merged 2 commits into
llvm:mainfrom
H-G-Hristov:hgh/libcxx/nodiscard-to-take_while

Conversation

@H-G-Hristov

@H-G-Hristov H-G-Hristov commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

@Zingam
Zingam marked this pull request as ready for review June 22, 2026 19:54
@Zingam
Zingam requested a review from a team as a code owner June 22, 2026 19:54
@llvmorg-github-actions llvmorg-github-actions Bot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jun 22, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

Changes

Towards #172124

References:


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 --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);
+}

@Zingam
Zingam merged commit 9b84d3b into llvm:main Jun 25, 2026
81 checks passed
@H-G-Hristov
H-G-Hristov deleted the hgh/libcxx/nodiscard-to-take_while branch June 27, 2026 19:54
maarcosrmz pushed a commit to maarcosrmz/llvm-project that referenced this pull request Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants