diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2025-01-23 11:25:22 +0100 |
|---|---|---|
| committer | Paolo Bonzini <pbonzini@redhat.com> | 2025-01-23 18:47:46 +0100 |
| commit | 7d0520398f7f58214cf5242b34c1b46efa2fcf4f (patch) | |
| tree | 1aa706baadc819ff3f569af00c91ba99fe4696fd /rust/hw/char/pl011/src/device_class.rs | |
| parent | 24f0e8d818b931758b6dc47f973a6b1b80ecee1f (diff) | |
| download | focaccia-qemu-7d0520398f7f58214cf5242b34c1b46efa2fcf4f.tar.gz focaccia-qemu-7d0520398f7f58214cf5242b34c1b46efa2fcf4f.zip | |
rust: prefer NonNull::new to assertions
Do not use new_unchecked; the effect is the same, but the code is easier to read and unsafe regions become smaller. Likewise, NonNull::new can be used instead of assertion and followed by as_ref() or as_mut() instead of dereferencing the pointer. Suggested-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/hw/char/pl011/src/device_class.rs')
| -rw-r--r-- | rust/hw/char/pl011/src/device_class.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/rust/hw/char/pl011/src/device_class.rs b/rust/hw/char/pl011/src/device_class.rs index b052d98803..6fa14ca0f9 100644 --- a/rust/hw/char/pl011/src/device_class.rs +++ b/rust/hw/char/pl011/src/device_class.rs @@ -12,12 +12,10 @@ use qemu_api::{ use crate::device::PL011State; +#[allow(clippy::missing_const_for_fn)] extern "C" fn pl011_clock_needed(opaque: *mut c_void) -> bool { - unsafe { - debug_assert!(!opaque.is_null()); - let state = NonNull::new_unchecked(opaque.cast::<PL011State>()); - state.as_ref().migrate_clock - } + let state = NonNull::new(opaque).unwrap().cast::<PL011State>(); + unsafe { state.as_ref().migrate_clock } } /// Migration subsection for [`PL011State`] clock. @@ -33,15 +31,12 @@ pub static VMSTATE_PL011_CLOCK: VMStateDescription = VMStateDescription { }; extern "C" fn pl011_post_load(opaque: *mut c_void, version_id: c_int) -> c_int { - unsafe { - debug_assert!(!opaque.is_null()); - let mut state = NonNull::new_unchecked(opaque.cast::<PL011State>()); - let result = state.as_mut().post_load(version_id as u32); - if result.is_err() { - -1 - } else { - 0 - } + let mut state = NonNull::new(opaque).unwrap().cast::<PL011State>(); + let result = unsafe { state.as_mut().post_load(version_id as u32) }; + if result.is_err() { + -1 + } else { + 0 } } |