]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: macros: simplify code using `feature(extract_if)`
authorMiguel Ojeda <ojeda@kernel.org>
Sun, 5 Apr 2026 23:52:51 +0000 (01:52 +0200)
committerMiguel Ojeda <ojeda@kernel.org>
Tue, 7 Apr 2026 08:00:24 +0000 (10:00 +0200)
`feature(extract_if)` [1] was stabilized in Rust 1.87.0 [2], and the last
significant change happened in Rust 1.85.0 [3] when the range parameter
was added.

That is, with our new minimum version, we can start using the feature.

Thus simplify the code using the feature and remove the TODO comment.

Suggested-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/rust-for-linux/DHHVSX66206Y.3E7I9QUNTCJ8I@garyguo.net/
Link: https://github.com/rust-lang/rust/issues/43244
Link: https://github.com/rust-lang/rust/pull/137109
Link: https://github.com/rust-lang/rust/pull/133265
Link: https://patch.msgid.link/20260405235309.418950-16-ojeda@kernel.org
Reviewed-by: Tamir Duberstein <tamird@kernel.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/macros/kunit.rs
rust/macros/lib.rs

index 6be880d634e266219b8c0208699c9e813d92ccad..ae20ed6768f1564439acf0cadc72a787bada8473 100644 (file)
@@ -87,10 +87,11 @@ 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 {
+        if f.attrs
+            .extract_if(.., |attr| attr.path().is_ident("test"))
+            .count()
+            == 0
+        {
             processed_items.push(Item::Fn(f));
             continue;
         }
index 0c36194d99716813629234e11158473b3f0a9256..2cfd59e0f9e7c9bf2f4b4114d95a24d3628454f6 100644 (file)
@@ -6,6 +6,9 @@
 // and thus add a dependency on `include/config/RUSTC_VERSION_TEXT`, which is
 // touched by Kconfig when the version string from the compiler changes.
 
+// Stable since Rust 1.87.0.
+#![feature(extract_if)]
+//
 // Stable since Rust 1.88.0 under a different name, `proc_macro_span_file`,
 // which was added in Rust 1.88.0. This is why `cfg_attr` is used here, i.e.
 // to avoid depending on the full `proc_macro_span` on Rust >= 1.88.0.