public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: kunit: implement test attr filtering without extract_if
@ 2026-02-25 19:45 Elsanti
  2026-02-25 21:07 ` Gary Guo
  0 siblings, 1 reply; 2+ messages in thread
From: Elsanti @ 2026-02-25 19:45 UTC (permalink / raw)
  To: brendan.higgins, davidgow, ojeda
  Cc: raemoar63, boqun, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, dakr, linux-kselftest, kunit-dev, rust-for-linux,
	linux-kernel, 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 <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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: kunit: implement test attr filtering without extract_if
  2026-02-25 19:45 [PATCH] rust: kunit: implement test attr filtering without extract_if Elsanti
@ 2026-02-25 21:07 ` Gary Guo
  0 siblings, 0 replies; 2+ messages in thread
From: Gary Guo @ 2026-02-25 21:07 UTC (permalink / raw)
  To: Elsanti
  Cc: brendan.higgins, davidgow, ojeda, raemoar63, boqun, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, linux-kselftest,
	kunit-dev, rust-for-linux, linux-kernel

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;
>          }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-02-25 21:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-25 19:45 [PATCH] rust: kunit: implement test attr filtering without extract_if Elsanti
2026-02-25 21:07 ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox