Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v2] rust: macros: drop underscores from `#[optional]` methods
@ 2026-11-30 16:44 Gary Guo via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Gary Guo via B4 Relay @ 2026-11-30 16:44 UTC (permalink / raw)
  To: Arnd Bergmann, Greg Kroah-Hartman, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, Gary Guo

From: Gary Guo <gary@garyguo.net>

In the `vtable` macro, `#[optional]` methods do not have a body, so they
should not require underscore-prefixing the function parameters just like
mandatory trait methods. Add `#[allow(unused_variables)]` to make
underscore unnecessary and drop the prefix from existing users.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/CANiq72neqitKHftQ9UVJaKLkcUdX8uAWLtoE08-G784mqX+H9w@mail.gmail.com/
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Gary Guo <gary@garyguo.net>
---
Changes in v2:
- Also remove prefix from compat_ioctl (Sashiko)
- Link to v1: https://patch.msgid.link/20261126-dev-optional-v1-1-9e8d3a807d9b@garyguo.net
---
 rust/kernel/miscdevice.rs | 32 ++++++++++++++------------------
 rust/macros/vtable.rs     |  2 ++
 2 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 900676ea6bbc..3a672f4ac914 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -179,18 +179,18 @@ fn release(device: Self::File, _file: &File) {
     /// `vma`.
     #[optional]
     fn mmap(
-        _device: <Self::File as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _vma: &VmaNew,
+        device: <Self::File as ForeignOwnable>::Borrowed<'_>,
+        file: &File,
+        vma: &VmaNew,
     ) -> Result;
 
     /// Read from this miscdevice.
     #[optional]
-    fn read_iter(_kiocb: Kiocb<'_, Self::File>, _iov: &mut IovIterDest<'_>) -> Result<usize>;
+    fn read_iter(kiocb: Kiocb<'_, Self::File>, iov: &mut IovIterDest<'_>) -> Result<usize>;
 
     /// Write to this miscdevice.
     #[optional]
-    fn write_iter(_kiocb: Kiocb<'_, Self::File>, _iov: &mut IovIterSource<'_>) -> Result<usize>;
+    fn write_iter(kiocb: Kiocb<'_, Self::File>, iov: &mut IovIterSource<'_>) -> Result<usize>;
 
     /// Handler for ioctls.
     ///
@@ -199,10 +199,10 @@ fn mmap(
     /// [`kernel::ioctl`]: mod@crate::ioctl
     #[optional]
     fn ioctl(
-        _device: <Self::File as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _cmd: u32,
-        _arg: usize,
+        device: <Self::File as ForeignOwnable>::Borrowed<'_>,
+        file: &File,
+        cmd: u32,
+        arg: usize,
     ) -> Result<isize>;
 
     /// Handler for ioctls.
@@ -215,19 +215,15 @@ fn ioctl(
     #[cfg(CONFIG_COMPAT)]
     #[optional]
     fn compat_ioctl(
-        _device: <Self::File as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _cmd: u32,
-        _arg: usize,
+        device: <Self::File as ForeignOwnable>::Borrowed<'_>,
+        file: &File,
+        cmd: u32,
+        arg: usize,
     ) -> Result<isize>;
 
     /// Show info for this fd.
     #[optional]
-    fn show_fdinfo(
-        _device: <Self::File as ForeignOwnable>::Borrowed<'_>,
-        _m: &SeqFile,
-        _file: &File,
-    );
+    fn show_fdinfo(device: <Self::File as ForeignOwnable>::Borrowed<'_>, m: &SeqFile, file: &File);
 }
 
 /// A vtable for the file operations of a Rust miscdevice.
diff --git a/rust/macros/vtable.rs b/rust/macros/vtable.rs
index 981ad2e49647..aa41cb2f3ba9 100644
--- a/rust/macros/vtable.rs
+++ b/rust/macros/vtable.rs
@@ -79,6 +79,8 @@ fn handle_trait(mut item: ItemTrait) -> Result<ItemTrait> {
                 }));
                 // Ensure that the function is never code generated unless used.
                 fn_item.attrs.push(parse_quote!(#[inline]));
+                // Injected default implementation does not use function parameters.
+                fn_item.attrs.push(parse_quote!(#[allow(unused_variables)]));
             }
 
             // We don't know on the implementation-site whether a method is required or provided

---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261126-dev-optional-50abc27e06cc

Best regards,
--  
Gary Guo <gary@garyguo.net>



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 16:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:44 [PATCH v2] rust: macros: drop underscores from `#[optional]` methods Gary Guo via B4 Relay

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