[PATCH] rust: bitmap: clean Rust 1.92.0 `unused_unsafe` warning

Miguel Ojeda posted 1 patch 2 months, 1 week ago
rust/kernel/bitmap.rs | 2 ++
1 file changed, 2 insertions(+)
[PATCH] rust: bitmap: clean Rust 1.92.0 `unused_unsafe` warning
Posted by Miguel Ojeda 2 months, 1 week ago
Starting with Rust 1.92.0 (expected 2025-12-11), Rust allows to safely
take the address of a union field [1][2]:

      CLIPPY L rust/kernel.o
    error: unnecessary `unsafe` block
       --> rust/kernel/bitmap.rs:169:13
        |
    169 |             unsafe { core::ptr::addr_of!(self.repr.bitmap) }
        |             ^^^^^^ unnecessary `unsafe` block
        |
        = note: `-D unused-unsafe` implied by `-D warnings`
        = help: to override `-D warnings` add `#[allow(unused_unsafe)]`

    error: unnecessary `unsafe` block
       --> rust/kernel/bitmap.rs:185:13
        |
    185 |             unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }
        |             ^^^^^^ unnecessary `unsafe` block

Thus allow both instances to clean the warning in newer compilers.

Link: https://github.com/rust-lang/rust/issues/141264 [1]
Link: https://github.com/rust-lang/rust/pull/141469 [2]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/bitmap.rs | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index f45915694454..711b8368b38f 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -166,6 +166,7 @@ impl core::ops::Deref for BitmapVec {
     fn deref(&self) -> &Bitmap {
         let ptr = if self.nbits <= BITS_PER_LONG {
             // SAFETY: Bitmap is represented inline.
+            #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
             unsafe { core::ptr::addr_of!(self.repr.bitmap) }
         } else {
             // SAFETY: Bitmap is represented as array of `unsigned long`.
@@ -182,6 +183,7 @@ impl core::ops::DerefMut for BitmapVec {
     fn deref_mut(&mut self) -> &mut Bitmap {
         let ptr = if self.nbits <= BITS_PER_LONG {
             // SAFETY: Bitmap is represented inline.
+            #[allow(unused_unsafe, reason = "Safe since Rust 1.92.0")]
             unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }
         } else {
             // SAFETY: Bitmap is represented as array of `unsigned long`.

base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
-- 
2.51.0
Re: [PATCH] rust: bitmap: clean Rust 1.92.0 `unused_unsafe` warning
Posted by Alice Ryhl 2 months, 1 week ago
On Mon, Oct 13, 2025 at 02:14:22AM +0200, Miguel Ojeda wrote:
> Starting with Rust 1.92.0 (expected 2025-12-11), Rust allows to safely
> take the address of a union field [1][2]:
> 
>       CLIPPY L rust/kernel.o
>     error: unnecessary `unsafe` block
>        --> rust/kernel/bitmap.rs:169:13
>         |
>     169 |             unsafe { core::ptr::addr_of!(self.repr.bitmap) }
>         |             ^^^^^^ unnecessary `unsafe` block
>         |
>         = note: `-D unused-unsafe` implied by `-D warnings`
>         = help: to override `-D warnings` add `#[allow(unused_unsafe)]`
> 
>     error: unnecessary `unsafe` block
>        --> rust/kernel/bitmap.rs:185:13
>         |
>     185 |             unsafe { core::ptr::addr_of_mut!(self.repr.bitmap) }
>         |             ^^^^^^ unnecessary `unsafe` block
> 
> Thus allow both instances to clean the warning in newer compilers.
> 
> Link: https://github.com/rust-lang/rust/issues/141264 [1]
> Link: https://github.com/rust-lang/rust/pull/141469 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>