[PATCH 8/9] rust: enable clippy::ptr_cast_constness

Paolo Bonzini posted 9 patches 6 months, 2 weeks ago
There is a newer version of this series
[PATCH 8/9] rust: enable clippy::ptr_cast_constness
Posted by Paolo Bonzini 6 months, 2 weeks ago
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 docs/devel/rust.rst             | 2 --
 rust/Cargo.toml                 | 2 +-
 rust/clippy.toml                | 2 +-
 rust/hw/char/pl011/Cargo.toml   | 2 +-
 rust/hw/timer/hpet/Cargo.toml   | 2 +-
 rust/hw/timer/hpet/src/hpet.rs  | 2 +-
 rust/qemu-api-macros/Cargo.toml | 2 +-
 rust/qemu-api/Cargo.toml        | 2 +-
 rust/qemu-api/src/cell.rs       | 2 +-
 rust/qemu-api/src/chardev.rs    | 2 +-
 rust/qemu-api/src/qom.rs        | 4 ++--
 rust/qemu-api/src/timer.rs      | 2 +-
 12 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index cae2634a2e9..92550d9d581 100644
--- a/docs/devel/rust.rst
+++ b/docs/devel/rust.rst
@@ -74,8 +74,6 @@ Supported tools
 QEMU supports rustc version 1.63.0 and newer.  Notably, the following features
 are missing:
 
-* ``cast_mut()``/``cast_const()`` (1.65.0).  Use ``as`` instead.
-
 * Generic Associated Types (1.65.0)
 
 * "Return position ``impl Trait`` in Traits" (1.75.0, blocker for including
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index ea2a85a1d07..bcf43718cf9 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -64,6 +64,7 @@ no_effect_underscore_binding = "deny"
 option_option = "deny"
 or_fun_call = "deny"
 ptr_as_ptr = "deny"
+ptr_cast_constness = "deny"
 pub_underscore_fields = "deny"
 redundant_clone = "deny"
 redundant_closure_for_method_calls = "deny"
@@ -85,7 +86,6 @@ used_underscore_binding = "deny"
 
 # nice to have, but cannot be enabled yet
 #wildcard_imports = "deny"   # still have many bindings::* imports
-#ptr_cast_constness = "deny" # needs 1.65.0 for cast_mut()/cast_const()
 
 # these may have false positives
 #option_if_let_else = "deny"
diff --git a/rust/clippy.toml b/rust/clippy.toml
index 5d190f91dec..933e46a2ffb 100644
--- a/rust/clippy.toml
+++ b/rust/clippy.toml
@@ -1,2 +1,2 @@
 doc-valid-idents = ["PrimeCell", ".."]
-msrv = "1.63.0"
+msrv = "1.77.0"
diff --git a/rust/hw/char/pl011/Cargo.toml b/rust/hw/char/pl011/Cargo.toml
index f2296cad58b..4766badd73e 100644
--- a/rust/hw/char/pl011/Cargo.toml
+++ b/rust/hw/char/pl011/Cargo.toml
@@ -9,7 +9,7 @@ resolver = "2"
 publish = false
 keywords = []
 categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
 
 [lib]
 crate-type = ["staticlib"]
diff --git a/rust/hw/timer/hpet/Cargo.toml b/rust/hw/timer/hpet/Cargo.toml
index 147f216e725..c6c48b2f887 100644
--- a/rust/hw/timer/hpet/Cargo.toml
+++ b/rust/hw/timer/hpet/Cargo.toml
@@ -5,7 +5,7 @@ edition = "2021"
 authors = ["Zhao Liu <zhao1.liu@intel.com>"]
 license = "GPL-2.0-or-later"
 description = "IA-PC High Precision Event Timer emulation in Rust"
-rust-version = "1.63.0"
+rust-version = "1.77.0"
 
 [lib]
 crate-type = ["staticlib"]
diff --git a/rust/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index 6afef083a57..779681d6509 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -218,7 +218,7 @@ fn init(&mut self, index: u8, state: &HPETState) {
             // SAFETY: the HPETTimer will only be used after the timer
             // is initialized below.
             qemu_timer: unsafe { Timer::new() },
-            state: NonNull::new(state as *const _ as *mut _).unwrap(),
+            state: NonNull::new((state as *const HPETState).cast_mut()).unwrap(),
             config: 0,
             cmp: 0,
             fsb: 0,
diff --git a/rust/qemu-api-macros/Cargo.toml b/rust/qemu-api-macros/Cargo.toml
index 89dee1cfb39..8344b1f939d 100644
--- a/rust/qemu-api-macros/Cargo.toml
+++ b/rust/qemu-api-macros/Cargo.toml
@@ -9,7 +9,7 @@ resolver = "2"
 publish = false
 keywords = []
 categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
 
 [lib]
 proc-macro = true
diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
index c5b7b2464e9..be00ef1ae9b 100644
--- a/rust/qemu-api/Cargo.toml
+++ b/rust/qemu-api/Cargo.toml
@@ -12,7 +12,7 @@ resolver = "2"
 publish = false
 keywords = []
 categories = []
-rust-version = "1.63.0"
+rust-version = "1.77.0"
 
 [dependencies]
 qemu_api_macros = { path = "../qemu-api-macros" }
diff --git a/rust/qemu-api/src/cell.rs b/rust/qemu-api/src/cell.rs
index b35d19723f6..05ce09f6cb6 100644
--- a/rust/qemu-api/src/cell.rs
+++ b/rust/qemu-api/src/cell.rs
@@ -1016,7 +1016,7 @@ pub const fn as_mut_ptr(&self) -> *mut T {
 
     /// Returns a raw pointer to the opaque data.
     pub const fn as_ptr(&self) -> *const T {
-        self.as_mut_ptr() as *const _
+        self.as_mut_ptr().cast_const()
     }
 
     /// Returns a raw pointer to the opaque data that can be passed to a
diff --git a/rust/qemu-api/src/chardev.rs b/rust/qemu-api/src/chardev.rs
index 146a4852da3..6e0590d758e 100644
--- a/rust/qemu-api/src/chardev.rs
+++ b/rust/qemu-api/src/chardev.rs
@@ -160,7 +160,7 @@ pub fn enable_handlers<
                 receive_cb,
                 event_cb,
                 None,
-                (owner as *const T as *mut T).cast::<c_void>(),
+                (owner as *const T).cast_mut().cast::<c_void>(),
                 core::ptr::null_mut(),
                 true,
             );
diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs
index 6929e4d33ae..9766a607e73 100644
--- a/rust/qemu-api/src/qom.rs
+++ b/rust/qemu-api/src/qom.rs
@@ -388,7 +388,7 @@ fn as_mut_ptr<U: ObjectType>(&self) -> *mut U
     {
         #[allow(clippy::as_ptr_cast_mut)]
         {
-            self.as_ptr::<U>() as *mut _
+            self.as_ptr::<U>().cast_mut()
         }
     }
 }
@@ -637,7 +637,7 @@ pub unsafe fn from_raw(ptr: *const T) -> Self {
         // SAFETY NOTE: while NonNull requires a mutable pointer, only
         // Deref is implemented so the pointer passed to from_raw
         // remains const
-        Owned(NonNull::new(ptr as *mut T).unwrap())
+        Owned(NonNull::new(ptr.cast_mut()).unwrap())
     }
 
     /// Obtain a raw C pointer from a reference.  `src` is consumed
diff --git a/rust/qemu-api/src/timer.rs b/rust/qemu-api/src/timer.rs
index d697fd742bc..868bd88575f 100644
--- a/rust/qemu-api/src/timer.rs
+++ b/rust/qemu-api/src/timer.rs
@@ -81,7 +81,7 @@ pub fn init_full<'timer, 'opaque: 'timer, T, F>(
                 scale as c_int,
                 attributes as c_int,
                 Some(timer_cb),
-                (opaque as *const T).cast::<c_void>() as *mut c_void,
+                (opaque as *const T).cast::<c_void>().cast_mut(),
             )
         }
     }
-- 
2.49.0
Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
Posted by Stefan Zabka 6 months, 2 weeks ago
Unrelated to the actual change, but a drive-by observation:
If you want to keep the MSRV in sync across all workspace members, you 
could use workspace.package.rust-version to define it once and inherit 
it everywhere. (Same goes for edition, license, and resolver.)

https://doc.rust-lang.org/cargo/reference/workspaces.html#the-package-table

I'd like to provide a patch instead of just heckling, but I have a 
couple of questions:

1. Is this actually desired behaviour, or might you want different 
values for these fields?
2. Should I implement this against qemu-project/qemu master branch or is 
there a dedicated Rust tree?
3. Should I wait with that until your change is landed so we don't conflict?

Best
Stefan
Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
Posted by Paolo Bonzini 6 months, 2 weeks ago
Il ven 2 mag 2025, 20:58 Stefan Zabka <git@zabka.it> ha scritto:

> Unrelated to the actual change, but a drive-by observation:
> If you want to keep the MSRV in sync across all workspace members, you
> could use workspace.package.rust-version to define it once and inherit
> it everywhere. (Same goes for edition, license, and resolver.)
>
> https://doc.rust-lang.org/cargo/reference/workspaces.html#the-package-table
>
> I'd like to provide a patch instead of just heckling, but I have a
> couple of questions:
>
> 1. Is this actually desired behaviour, or might you want different
> values for these fields?
>

Yes, it is.

2. Should I implement this against qemu-project/qemu master branch or is
> there a dedicated Rust tree?
>

Sure, go ahead! I will rebase on top, since I have to resend anyway.

Paolo

3. Should I wait with that until your change is landed so we don't conflict?
>
> Best
> Stefan
>
>
Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
Posted by Manos Pitsidianakis 6 months, 2 weeks ago
On Fri, 02 May 2025 13:23, Paolo Bonzini <pbonzini@redhat.com> wrote:
>Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>---
> docs/devel/rust.rst             | 2 --
> rust/Cargo.toml                 | 2 +-
> rust/clippy.toml                | 2 +-
> rust/hw/char/pl011/Cargo.toml   | 2 +-
> rust/hw/timer/hpet/Cargo.toml   | 2 +-
> rust/hw/timer/hpet/src/hpet.rs  | 2 +-
> rust/qemu-api-macros/Cargo.toml | 2 +-
> rust/qemu-api/Cargo.toml        | 2 +-
> rust/qemu-api/src/cell.rs       | 2 +-
> rust/qemu-api/src/chardev.rs    | 2 +-
> rust/qemu-api/src/qom.rs        | 4 ++--
> rust/qemu-api/src/timer.rs      | 2 +-
> 12 files changed, 12 insertions(+), 14 deletions(-)
>

These:

>diff --git a/rust/clippy.toml b/rust/clippy.toml
>index 5d190f91dec..933e46a2ffb 100644
>--- a/rust/clippy.toml
>+++ b/rust/clippy.toml
>@@ -1,2 +1,2 @@
> doc-valid-idents = ["PrimeCell", ".."]
>-msrv = "1.63.0"
>+msrv = "1.77.0"

>diff --git a/rust/hw/char/pl011/Cargo.toml b/rust/hw/char/pl011/Cargo.toml
>index f2296cad58b..4766badd73e 100644
>--- a/rust/hw/char/pl011/Cargo.toml
>+++ b/rust/hw/char/pl011/Cargo.toml
>@@ -9,7 +9,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
> 
> [lib]
> crate-type = ["staticlib"]

>diff --git a/rust/hw/timer/hpet/Cargo.toml b/rust/hw/timer/hpet/Cargo.toml
>index 147f216e725..c6c48b2f887 100644
>--- a/rust/hw/timer/hpet/Cargo.toml
>+++ b/rust/hw/timer/hpet/Cargo.toml
>@@ -5,7 +5,7 @@ edition = "2021"
> authors = ["Zhao Liu <zhao1.liu@intel.com>"]
> license = "GPL-2.0-or-later"
> description = "IA-PC High Precision Event Timer emulation in Rust"
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
> 
> [lib]
> crate-type = ["staticlib"]

>diff --git a/rust/qemu-api-macros/Cargo.toml 
>b/rust/qemu-api-macros/Cargo.toml
>index 89dee1cfb39..8344b1f939d 100644
>--- a/rust/qemu-api-macros/Cargo.toml
>+++ b/rust/qemu-api-macros/Cargo.toml
>@@ -9,7 +9,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
> 
> [lib]
> proc-macro = true
>diff --git a/rust/qemu-api/Cargo.toml b/rust/qemu-api/Cargo.toml
>index c5b7b2464e9..be00ef1ae9b 100644
>--- a/rust/qemu-api/Cargo.toml
>+++ b/rust/qemu-api/Cargo.toml
>@@ -12,7 +12,7 @@ resolver = "2"
> publish = false
> keywords = []
> categories = []
>-rust-version = "1.63.0"
>+rust-version = "1.77.0"
> 
> [dependencies]
> qemu_api_macros = { path = "../qemu-api-macros" }

msrv/rust-version bumps could instead go in the patch that also updates 
meson.build version.

Otherwise,

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Re: [PATCH 8/9] rust: enable clippy::ptr_cast_constness
Posted by Paolo Bonzini 6 months, 2 weeks ago
On Fri, May 2, 2025 at 1:13 PM Manos Pitsidianakis
<manos.pitsidianakis@linaro.org> wrote:
> msrv/rust-version bumps could instead go in the patch that also updates
> meson.build version.

Good idea, I was mistaken that ptr_cast_constness was already enabled
by bumping them, but it's actually default-allow.

Paolo