[PATCH] rust: kunit: implement test attr filtering without extract_if

0 views
Skip to first unread message

Elsanti

unread,
Feb 25, 2026, 2:45:57 PM (2 days ago) Feb 25
to brendan...@linux.dev, davi...@google.com, oj...@kernel.org, raem...@gmail.com, bo...@kernel.org, ga...@garyguo.net, bjor...@protonmail.com, los...@kernel.org, a.hin...@kernel.org, alic...@google.com, tmg...@umich.edu, da...@kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, rust-fo...@vger.kernel.org, linux-...@vger.kernel.org, Elsanti
Replace the TODO-only removal of #[test] attributes with a
retain_mut-based filter that works with the current MSRV.
This preserves test detection without depending on
Vec::extract_if and keeps the generated KUnit wrappers correct.

Signed-off-by: Elsanti <santiagoj...@gmail.com>
---
rust/macros/kunit.rs | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index 6be880d634e2..e064419bfc10 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -87,10 +87,15 @@ pub(crate) fn kunit_tests(test_suite: Ident, mut module: ItemMod) -> Result<Toke
continue;
};

- // TODO: Replace below with `extract_if` when MSRV is bumped above 1.85.
- let before_len = f.attrs.len();
- f.attrs.retain(|attr| !attr.path().is_ident("test"));
- if f.attrs.len() == before_len {
+ let mut had_test_attr = false;
+ f.attrs.retain_mut(|attr| {
+ let is_test = attr.path().is_ident("test");
+ if is_test {
+ had_test_attr = true;
+ }
+ !is_test
+ });
+ if !had_test_attr {
processed_items.push(Item::Fn(f));
continue;
}
--
2.53.0

Gary Guo

unread,
Feb 25, 2026, 4:08:00 PM (2 days ago) Feb 25
to Elsanti, brendan...@linux.dev, davi...@google.com, oj...@kernel.org, raem...@gmail.com, bo...@kernel.org, bjor...@protonmail.com, los...@kernel.org, a.hin...@kernel.org, alic...@google.com, tmg...@umich.edu, da...@kernel.org, linux-k...@vger.kernel.org, kuni...@googlegroups.com, rust-fo...@vger.kernel.org, linux-...@vger.kernel.org
Eh, no. I think the current code is more readable. I added the TODO because I think

if f.attrs.extract_if(|attr| attr.path().is_ident("test")).count() == 0 {
...
}

is slightly more readable, not that I had any issue with length comparison.

Best,
Gary
Reply all
Reply to author
Forward
0 new messages