public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: device: allow `dead_code` for `Device<>::parent()`
@ 2025-04-29 15:03 Miguel Ojeda
  2025-04-29 18:15 ` Danilo Krummrich
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2025-04-29 15:03 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	linux-kernel, patches

When `CONFIG_AUXILIARY_BUS` is disabled, `parent()` is still dead code:

    error: method `parent` is never used
      --> rust/kernel/device.rs:71:19
       |
    64 | impl<Ctx: DeviceContext> Device<Ctx> {
       | ------------------------------------ method in this implementation
    ...
    71 |     pub(crate) fn parent(&self) -> Option<&Self> {
       |                   ^^^^^^
       |
       = note: `-D dead-code` implied by `-D warnings`
       = help: to override `-D warnings` add `#[allow(dead_code)]`

Thus reintroduce the `expect`, but now as an `allow`, and do so as
`dead_code` since that is narrower.

Fixes: ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
I should have noticed this earlier, sorry. Please feel free to rebase to
fix it directly there if you prefer.

 rust/kernel/device.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 40c1f549b0ba..e8ab4b9617db 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -68,6 +68,7 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device {
     }

     /// Returns a reference to the parent device, if any.
+    #[allow(dead_code)]
     pub(crate) fn parent(&self) -> Option<&Self> {
         // SAFETY:
         // - By the type invariant `self.as_raw()` is always valid.

base-commit: ce735e73dd59b169b877cedd0753297c81c2a091
--
2.49.0

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

* Re: [PATCH] rust: device: allow `dead_code` for `Device<>::parent()`
  2025-04-29 15:03 [PATCH] rust: device: allow `dead_code` for `Device<>::parent()` Miguel Ojeda
@ 2025-04-29 18:15 ` Danilo Krummrich
  2025-04-29 19:05   ` Miguel Ojeda
  0 siblings, 1 reply; 5+ messages in thread
From: Danilo Krummrich @ 2025-04-29 18:15 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Alex Gaynor, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, rust-for-linux, linux-kernel, patches

On Tue, Apr 29, 2025 at 05:03:46PM +0200, Miguel Ojeda wrote:
> When `CONFIG_AUXILIARY_BUS` is disabled, `parent()` is still dead code:
> 
>     error: method `parent` is never used
>       --> rust/kernel/device.rs:71:19
>        |
>     64 | impl<Ctx: DeviceContext> Device<Ctx> {
>        | ------------------------------------ method in this implementation
>     ...
>     71 |     pub(crate) fn parent(&self) -> Option<&Self> {
>        |                   ^^^^^^
>        |
>        = note: `-D dead-code` implied by `-D warnings`
>        = help: to override `-D warnings` add `#[allow(dead_code)]`
> 
> Thus reintroduce the `expect`, but now as an `allow`, and do so as
> `dead_code` since that is narrower.
> 
> Fixes: ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Good catch, thanks!

I wonder if for such or similar cases we want something like

	macro_rules! cfg_expect {
	    (not($cfg:meta) => $lint:ident, $item:item) => {
		#[cfg(not($cfg))]
		#[expect($lint)]
		$item

		#[cfg($cfg)]
		$item
	    };
	}

which could be used like this:

    cfg_expect!(not(CONFIG_FEATURE) => dead_code,
        pub(crate) fn foo(&self) {
            // noop
        }
    );

Maybe there is a much better solution I don't know about.

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

* Re: [PATCH] rust: device: allow `dead_code` for `Device<>::parent()`
  2025-04-29 18:15 ` Danilo Krummrich
@ 2025-04-29 19:05   ` Miguel Ojeda
  2025-04-29 20:55     ` Danilo Krummrich
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2025-04-29 19:05 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Miguel Ojeda, Greg Kroah-Hartman, Rafael J. Wysocki, Alex Gaynor,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	linux-kernel, patches

On Tue, Apr 29, 2025 at 8:15 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> Maybe there is a much better solution I don't know about.

There is `cfg_attr` for that, and we use it in a couple places, e.g.

    #[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]

But unless we are more or less sure the condition will only ever
depend on a single `cfg` or so, I am not sure if it is worth the
complexity (and even then reasonable people could disagree).

I wrote about that approach in the "Conditional compilation" in one of
the bullets of:

    https://docs.kernel.org/rust/coding-guidelines.html#lints

I hope that helps!

Cheers,
Miguel

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

* Re: [PATCH] rust: device: allow `dead_code` for `Device<>::parent()`
  2025-04-29 19:05   ` Miguel Ojeda
@ 2025-04-29 20:55     ` Danilo Krummrich
  2025-04-29 21:05       ` Miguel Ojeda
  0 siblings, 1 reply; 5+ messages in thread
From: Danilo Krummrich @ 2025-04-29 20:55 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Miguel Ojeda, Greg Kroah-Hartman, Rafael J. Wysocki, Alex Gaynor,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	linux-kernel, patches

On Tue, Apr 29, 2025 at 09:05:21PM +0200, Miguel Ojeda wrote:
> On Tue, Apr 29, 2025 at 8:15 PM Danilo Krummrich <dakr@kernel.org> wrote:
> >
> > Maybe there is a much better solution I don't know about.
> 
> There is `cfg_attr` for that, and we use it in a couple places, e.g.
> 
>     #[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]

Well, that is much better, thanks!

I prefer this then, I really want to catch if some other code starts using this.

- Danilo

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

* Re: [PATCH] rust: device: allow `dead_code` for `Device<>::parent()`
  2025-04-29 20:55     ` Danilo Krummrich
@ 2025-04-29 21:05       ` Miguel Ojeda
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-04-29 21:05 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Miguel Ojeda, Greg Kroah-Hartman, Rafael J. Wysocki, Alex Gaynor,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	linux-kernel, patches

On Tue, Apr 29, 2025 at 10:55 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> Well, that is much better, thanks!
>
> I prefer this then, I really want to catch if some other code starts using this.

You're welcome! I will send v2 then, in case it is not "fixup"ed.

Cheers,
Miguel

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

end of thread, other threads:[~2025-04-29 21:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-29 15:03 [PATCH] rust: device: allow `dead_code` for `Device<>::parent()` Miguel Ojeda
2025-04-29 18:15 ` Danilo Krummrich
2025-04-29 19:05   ` Miguel Ojeda
2025-04-29 20:55     ` Danilo Krummrich
2025-04-29 21:05       ` Miguel Ojeda

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