Re: [PATCH v15 3/3] rust: platform: add resource accessors

From: Danilo Krummrich
Date: Sat Jul 19 2025 - 09:15:56 EST


On Thu Jul 17, 2025 at 5:55 PM CEST, Daniel Almeida wrote:
> +impl Device<Bound> {
> + /// Returns an `IoRequest` for the resource at `index`, if any.
> + pub fn io_request_by_index(&self, index: u32) -> Option<IoRequest<'_>> {
> + // SAFETY: `resource` is a valid resource for `&self` during the
> + // lifetime of the `IoRequest`.
> + self.resource_by_index(index)
> + .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })

It seems there's a bug in some clippy versions we support. My regular testing
shows a warning for this with clippy 1.78:

warning: unsafe block missing a safety comment
--> rust/kernel/platform.rs:275:29
|
275 | .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|

However, there clearly is a safety comment.

It seems it gets confused by the multiline formatting. If I write this as

self.resource_by_index(index)
// SAFETY: `resource` is a valid resource for `&self` during the
// lifetime of the `IoRequest`.
.map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })

the warning goes away.

@Miguel: What's the preferred way dealing with this? I assume we just want to
ignore this warning for the affected compiler versions?

> + }
> +
> + /// Returns an `IoRequest` for the resource with a given `name`, if any.
> + pub fn io_request_by_name(&self, name: &CStr) -> Option<IoRequest<'_>> {
> + // SAFETY: `resource` is a valid resource for `&self` during the
> + // lifetime of the `IoRequest`.
> + self.resource_by_name(name)
> + .map(|resource| unsafe { IoRequest::new(self.as_ref(), resource) })
> + }
> }