Skip to contentSkip to conversation

rust: macros: drop underscores from `#[optional]` methods

PATCH In review Gary Guo 2 participants 2 messages Nov 26

Subsystems (2)
lore
Share & tools
lore

Gary Guo

author reviewer
#0 lore raw

PATCH 1/1 rust: macros: drop underscores from `#[optional]` methods

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.

---
 rust/kernel/miscdevice.rs | 24 ++++++++++--------------
 rust/macros/vtable.rs     |  2 ++
 2 files changed, 12 insertions(+), 14 deletions(-)
rust/kernel/miscdevice.rs +10 -14
index 900676ea6bbc..8e9ecb3b72c2 100644
@@ -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.
@@ -223,11 +223,7 @@ fn compat_ioctl(
 
     /// 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.
rust/macros/vtable.rs +2 -0
index 981ad2e49647..aa41cb2f3ba9 100644
@@ -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: 7cfbf79c66c63e807ea452abf0546e6f6d6833a6
change-id: 20261126-dev-optional-50abc27e06cc

Best regards,

Signature · 1 line
Gary Guo <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Gary Guo

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.