[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to Range access (PR #173550)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 25 03:55:19 PST 2025
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/173550
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/range.access
Towards #172124
>From 873f50dfe96c065ccf8d0d4f9e9516808583545d Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Thu, 25 Dec 2025 13:54:56 +0200
Subject: [PATCH] [libc++][ranges] Applied `[[nodiscard]]` to Range access
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/range.access
Towards #172124
---
libcxx/include/__ranges/data.h | 4 +-
.../range.access/nodiscard.verify.cpp | 110 ++++++++++++++++++
2 files changed, 112 insertions(+), 2 deletions(-)
create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.access/nodiscard.verify.cpp
diff --git a/libcxx/include/__ranges/data.h b/libcxx/include/__ranges/data.h
index 50db3cffeeed8..cb3b826fceb23 100644
--- a/libcxx/include/__ranges/data.h
+++ b/libcxx/include/__ranges/data.h
@@ -51,12 +51,12 @@ concept __ranges_begin_invocable = !__member_data<_Tp> && __can_borrow<_Tp> && r
struct __fn {
template <__member_data _Tp>
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const noexcept(noexcept(__t.data())) {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const noexcept(noexcept(__t.data())) {
return __t.data();
}
template <__ranges_begin_invocable _Tp>
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
noexcept(noexcept(std::to_address(ranges::begin(__t)))) {
return std::to_address(ranges::begin(__t));
}
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.access/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.access/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..51a30154a6f1f
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.access/nodiscard.verify.cpp
@@ -0,0 +1,110 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+struct BorrowedRange {
+ int* begin() const { return nullptr; }
+};
+
+template <>
+inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange> = true;
+
+void test() {
+ // [range.access.begin]
+
+ // [range.access.end]
+
+ // [range.access.cbegin]
+
+ // [range.access.cend]
+
+ // [range.access.rbegin]
+
+ // [range.access.rend]
+
+ // [range.access.crbegin]
+
+ // [range.access.crend]
+
+ // [range.prim.size]
+
+ // [range.prim.ssize]
+
+ // [range.prim.size.hint]
+
+ // [range.prim.empty]
+
+ {
+ struct EmptyMemberRange {
+ int* begin();
+ bool empty() const { return true; };
+ };
+
+ struct SizeMemberRange {
+ int* begin();
+ size_t size() const { return 0; }
+ };
+
+ struct BeginEndComparableRange {
+ struct Sentinel {
+ constexpr bool operator==(int*) const { return true; }
+ };
+
+ int* begin() { return nullptr; };
+ Sentinel end() { return {}; };
+ };
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::empty(EmptyMemberRange{});
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::empty(SizeMemberRange{});
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::empty(BeginEndComparableRange{});
+ }
+
+ // [range.prim.data]
+
+ {
+ struct DataMemberRange {
+ int* begin();
+ int* data() { return nullptr; }
+ } dataMemberRange;
+
+ struct Range {
+ int* begin() { return nullptr; }
+ } range;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::data(dataMemberRange);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::data(range);
+ }
+
+ // [range.prim.cdata]
+
+ {
+ struct Range {
+ int* begin() const { return nullptr; }
+ } range;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::cdata(range);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::cdata(BorrowedRange{});
+ }
+}
More information about the libcxx-commits
mailing list