From 7d13f36eadab6a64ddd664ee62dad01bd87eef23 Mon Sep 17 00:00:00 2001 From: Alexey Bashtanov Date: Fri, 27 Sep 2024 16:48:21 +0100 Subject: [PATCH] ssx/async_algorithm: disallow passing a coroutine to ssx::async_for_each In fact fail the compliation if the function returns non-void [[nodiscard]], which holds for coroutines thanks to ss::future class [[nodiscard]] annotation. Calling ssx::async_for_each of course does not make much sense, as its purpose is to inject scheduling points. However it can be done by mistake, e.g. when a function return type was changed from void to ss::future<>. --- src/v/ssx/async_algorithm.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/v/ssx/async_algorithm.h b/src/v/ssx/async_algorithm.h index 10dcad1f97aee..ac12dbaa18a4b 100644 --- a/src/v/ssx/async_algorithm.h +++ b/src/v/ssx/async_algorithm.h @@ -106,6 +106,10 @@ template iter_size for_each_limit(const I begin, const I end, ssize_t limit, Fn f) { auto chunk_size = std::min(limit, end - begin); I chunk_end = begin + chunk_size; + // std::for_each discards [[nodiscard]] callable result, check explicitly + if (false) { + f(*begin); + } std::for_each(begin, chunk_end, std::move(f)); return {chunk_end, chunk_size}; }