[libcxx-commits] [libcxx] [libc++] Implement `ranges::fold_left_first` and `ranges::fold_left_first_with_iter` (PR #180214)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Apr 17 23:23:17 PDT 2026
================
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <algorithm>
+#include <functional>
+#include <vector>
+
+void test() {
+ auto const data = std::vector<int>{};
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_with_iter(data.begin(), data.end(), 0, std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_with_iter(data, 0, std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left(data.begin(), data.end(), 0, std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left(data, 0, std::plus());
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first_with_iter(data.begin(), data.end(), std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first_with_iter(data, std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first(data.begin(), data.end(), std::plus());
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::fold_left_first(data, std::plus());
----------------
H-G-Hristov wrote:
`[nodiscard]` tests are libc++ specific and therefore belong in `test/libcxx`. These cases should go in https://github.com/llvm/llvm-project/blob/40333cde2bbd62936d67478eab15ed1fa55b0532/libcxx/test/libcxx/algorithms/nodiscard.verify.cpp#L397.
We also try to order them in the order they appear in the implementation file.
https://github.com/llvm/llvm-project/pull/180214
More information about the libcxx-commits
mailing list