[libcxx-commits] [libcxx] [libc++][ranges] Refactored some `[[nodiscard]]` tests (PR #173574)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 25 09:52:39 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
`[[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.utility.conv
- https://wg21.link/range.utility.conv.to
- https://wg21.link/range.utility.conv.adaptors
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/173574.diff
2 Files Affected:
- (modified) libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp (-21)
- (added) libcxx/test/libcxx/ranges/range.utility/range.utility.conv/nodiscard.verify.cpp (+54)
``````````diff
diff --git a/libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp
index ecdad4ae9ec64..0700d55e28f5b 100644
--- a/libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp
@@ -13,16 +13,12 @@
// clang-format off
#include <ranges>
-#include <functional>
#include <vector>
#include "test_macros.h"
void test() {
std::vector<int> range;
- std::ranges::less_equal pred;
-
- std::views::drop(pred); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::views::split(range, 3); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::views::split(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -31,27 +27,10 @@ void test() {
std::views::take(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#if TEST_STD_VER >= 23
- std::views::drop(std::views::repeat(1)); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-
std::views::repeat(1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::views::repeat(1, std::unreachable_sentinel); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- auto rvalue_view = std::views::as_rvalue(range);
- std::views::as_rvalue(range); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::views::as_rvalue(rvalue_view); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-
std::views::take(std::views::repeat(3), 3); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::views::take(std::views::repeat(3, std::unreachable_sentinel), 3); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-
- std::allocator<int> alloc;
-
- std::ranges::to<std::vector<int>>(range); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::ranges::to<std::vector<int>>(range, alloc); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::ranges::to<std::vector>(range); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::ranges::to<std::vector>(range, alloc); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- range | std::ranges::to<std::vector<int>>(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- range | std::ranges::to<std::vector<int>>(alloc); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- range | std::ranges::to<std::vector>(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- range | std::ranges::to<std::vector>(alloc); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
#endif // TEST_STD_VER >= 23
}
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..76579ef332544
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/nodiscard.verify.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++23
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <vector>
+
+void test() {
+ std::vector<int> range;
+ std::allocator<int> alloc;
+
+ { // `ranges::to` base template -- the `_Container` type is a simple type template parameter.
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector<int>>(range);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector<int>>(range, alloc);
+ }
+
+ { // `ranges::to` specialization -- `_Container` is a template template parameter requiring deduction to figure out the
+ // container element type.
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector>(range);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector>(range, alloc);
+ }
+
+ { // Range adaptor closure object 1 -- wrapping the `ranges::to` version where `_Container` is a simple type template
+ // parameter.
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector<int>>();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector<int>>(alloc);
+ }
+
+ { // Range adaptor closure object 2 -- wrapping the `ranges::to` version where `_Container` is a template template
+ // parameter.
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector>();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::to<std::vector>(alloc);
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/173574
More information about the libcxx-commits
mailing list