[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `empty_view` (PR #173215)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 21 21:09:45 PST 2025


https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/173215

`[[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/ranges
- https://wg21.link/range.empty

Towards #172124

>From 22317d3dc7649a9129c745f310d9648812db1a73 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Mon, 22 Dec 2025 07:09:06 +0200
Subject: [PATCH] [libc++][ranges] Applied `[[nodiscard]]` to `empty_view`

`[[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/ranges
- https://wg21.link/range.empty

Towards #172124
---
 libcxx/include/__ranges/empty_view.h          | 10 +++----
 .../range.empty/nodiscard.verify.cpp          | 30 +++++++++++++++++++
 2 files changed, 35 insertions(+), 5 deletions(-)
 create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp

diff --git a/libcxx/include/__ranges/empty_view.h b/libcxx/include/__ranges/empty_view.h
index fc08492110f53..54d62b3c77a78 100644
--- a/libcxx/include/__ranges/empty_view.h
+++ b/libcxx/include/__ranges/empty_view.h
@@ -29,11 +29,11 @@ template <class _Tp>
   requires is_object_v<_Tp>
 class empty_view : public view_interface<empty_view<_Tp>> {
 public:
-  _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* begin() noexcept { return nullptr; }
-  _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* end() noexcept { return nullptr; }
-  _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* data() noexcept { return nullptr; }
-  _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 0; }
-  _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return true; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* begin() noexcept { return nullptr; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* end() noexcept { return nullptr; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr _Tp* data() noexcept { return nullptr; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 0; }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return true; }
 };
 
 template <class _Tp>
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..7d62b57d7f92e
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.empty/nodiscard.verify.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+void test() {
+  auto v = std::views::empty<int>;
+
+  // [range.empty.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}}
+  v.end();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.data();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.size();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.empty();
+}



More information about the libcxx-commits mailing list