From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: manos.pitsidianakis@linaro.org, qemu-rust@nongnu.org
Subject: [PATCH 08/11] rust: enable clippy::ptr_cast_constness
Date: Mon, 5 May 2025 11:04:33 +0200 [thread overview]
Message-ID: <20250505090438.24992-9-pbonzini@redhat.com> (raw)
In-Reply-To: <20250505090438.24992-1-pbonzini@redhat.com>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
docs/devel/rust.rst | 2 --
rust/Cargo.toml | 2 +-
rust/hw/timer/hpet/src/hpet.rs | 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 +-
7 files changed, 7 insertions(+), 9 deletions(-)
diff --git a/docs/devel/rust.rst b/docs/devel/rust.rst
index 13a002cfe69..a315f668865 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)
* ``CStr::from_bytes_with_nul()`` as a ``const`` function (1.72.0).
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index eda7980b31a..a328634d949 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -71,6 +71,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"
@@ -92,7 +93,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/hw/timer/hpet/src/hpet.rs b/rust/hw/timer/hpet/src/hpet.rs
index d4364f2f2f7..b7a1be05deb 100644
--- a/rust/hw/timer/hpet/src/hpet.rs
+++ b/rust/hw/timer/hpet/src/hpet.rs
@@ -219,7 +219,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/src/cell.rs b/rust/qemu-api/src/cell.rs
index ab0785a2692..1cb9fdcd53f 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 52e3a1ec981..41e5a5e29a8 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()
}
}
}
@@ -638,7 +638,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
next prev parent reply other threads:[~2025-05-05 9:06 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-05 9:04 [PATCH 00/11] rust: allow minimum version of 1.77 Paolo Bonzini
2025-05-05 9:04 ` [PATCH 01/11] lcitool: use newer Rust for Debian and Ubuntu Paolo Bonzini
2025-05-05 9:04 ` [PATCH 02/11] meson, cargo: require Rust 1.77.0 Paolo Bonzini
2025-05-05 9:29 ` Manos Pitsidianakis
2025-05-06 7:39 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 03/11] rust: use std::ffi instead of std::os::raw Paolo Bonzini
2025-05-06 7:40 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 04/11] rust: let bilge use "let ... else" Paolo Bonzini
2025-05-06 7:41 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 05/11] rust: qemu_api_macros: make pattern matching more readable and efficient Paolo Bonzini
2025-05-06 7:48 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 06/11] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
2025-05-06 7:56 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 07/11] rust: qom: fix TODO about zeroability of classes Paolo Bonzini
2025-05-05 9:32 ` Manos Pitsidianakis
2025-05-06 8:01 ` Zhao Liu
2025-05-05 9:04 ` Paolo Bonzini [this message]
2025-05-06 8:04 ` [PATCH 08/11] rust: enable clippy::ptr_cast_constness Zhao Liu
2025-05-05 9:04 ` [PATCH 09/11] rust: remove offset_of replacement Paolo Bonzini
2025-05-05 9:34 ` Manos Pitsidianakis
2025-05-06 8:32 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 10/11] rust: replace c_str! with c"" literals Paolo Bonzini
2025-05-06 8:34 ` Zhao Liu
2025-05-05 9:04 ` [PATCH 11/11] docs: rust: update for newer minimum supported version Paolo Bonzini
2025-05-05 9:35 ` Manos Pitsidianakis
2025-05-06 8:38 ` Zhao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250505090438.24992-9-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=manos.pitsidianakis@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).