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

Elsanti posted 1 patch 1 month, 2 weeks ago
rust/macros/kunit.rs | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
[PATCH] rust: kunit: implement test attr filtering without extract_if
Posted by Elsanti 1 month, 2 weeks ago
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 <santiagojoseleal27@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
Re: [PATCH] rust: kunit: implement test attr filtering without extract_if
Posted by Gary Guo 1 month, 2 weeks ago
On 2026-02-25 19:45, Elsanti wrote:
> 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 <santiagojoseleal27@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
> +        });

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

> +        if !had_test_attr {
>              processed_items.push(Item::Fn(f));
>              continue;
>          }