[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `istream_view` (PR #205154)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jun 25 19:59:45 PDT 2026
https://github.com/Zingam updated https://github.com/llvm/llvm-project/pull/205154
>From 568618d53b12adc7b730a856c16bc4b6a25a96ad Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Jun 2026 21:08:49 +0300
Subject: [PATCH] [libc++][ranges] Applied `[[nodiscard]]` to `istream_view`
Towards #172124
References:
- https://wg21.link/range.istream
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
---
libcxx/include/__ranges/istream_view.h | 8 ++--
.../range.istream.view/nodiscard.verify.cpp | 41 +++++++++++++++++++
2 files changed, 45 insertions(+), 4 deletions(-)
create mode 100644 libcxx/test/libcxx/ranges/range.factories/range.istream.view/nodiscard.verify.cpp
diff --git a/libcxx/include/__ranges/istream_view.h b/libcxx/include/__ranges/istream_view.h
index ab05cb6ef1fe3..7c64463108e1a 100644
--- a/libcxx/include/__ranges/istream_view.h
+++ b/libcxx/include/__ranges/istream_view.h
@@ -46,12 +46,12 @@ class basic_istream_view : public view_interface<basic_istream_view<_Val, _CharT
_LIBCPP_HIDE_FROM_ABI constexpr explicit basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
: __stream_(std::addressof(__stream)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
*__stream_ >> __value_;
return __iterator{*this};
}
- _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
private:
basic_istream<_CharT, _Traits>* __stream_;
@@ -82,7 +82,7 @@ class basic_istream_view<_Val, _CharT, _Traits>::__iterator {
_LIBCPP_HIDE_FROM_ABI void operator++(int) { ++*this; }
- _LIBCPP_HIDE_FROM_ABI _Val& operator*() const { return __parent_->__value_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Val& operator*() const { return __parent_->__value_; }
_LIBCPP_HIDE_FROM_ABI friend bool operator==(const __iterator& __x, default_sentinel_t) {
return !*__x.__get_parent_stream();
@@ -113,7 +113,7 @@ struct __fn {
template <class _Up, class _UnCVRef = remove_cvref_t<_Up>>
requires derived_from<_UnCVRef, basic_istream<typename _UnCVRef::char_type,
typename _UnCVRef::traits_type>>
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Up&& __u) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Up&& __u) const
noexcept(noexcept(basic_istream_view<_Tp, typename _UnCVRef::char_type,
typename _UnCVRef::traits_type>(std::forward<_Up>(__u))))
-> decltype( basic_istream_view<_Tp, typename _UnCVRef::char_type,
diff --git a/libcxx/test/libcxx/ranges/range.factories/range.istream.view/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.factories/range.istream.view/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..7f93356da4cbc
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.factories/range.istream.view/nodiscard.verify.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: no-localization
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <sstream>
+#include <utility>
+
+void test() {
+ std::stringstream ss;
+ auto v = std::views::istream<char>(ss);
+
+ // [range.istream.view]
+
+ // 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}}
+ std::as_const(v).end();
+
+ // [range.istream.iterator]
+
+ auto it = v.begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *it;
+
+ // [range.istream.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::istream<char>(ss);
+}
More information about the libcxx-commits
mailing list