Skip to content

Commit 9917f1a

Browse files
Merge pull request #1455 from rust-osdev/bishop-from-ref
uefi: Deny clippy::ref_as_ptr
2 parents 0b77782 + 81328dc commit 9917f1a

File tree

9 files changed

+25
-22
lines changed

9 files changed

+25
-22
lines changed

uefi/src/boot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ pub fn locate_handle<'buf>(
790790
SearchType::ByRegisterNotify(registration) => {
791791
(1, ptr::null(), registration.0.as_ptr().cast_const())
792792
}
793-
SearchType::ByProtocol(guid) => (2, guid as *const Guid, ptr::null()),
793+
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),
794794
};
795795

796796
let mut buffer_size = buffer.len() * mem::size_of::<Handle>();
@@ -829,7 +829,7 @@ pub fn locate_handle_buffer(search_ty: SearchType) -> Result<HandleBuffer> {
829829
SearchType::ByRegisterNotify(registration) => {
830830
(1, ptr::null(), registration.0.as_ptr().cast_const())
831831
}
832-
SearchType::ByProtocol(guid) => (2, guid as *const _, ptr::null()),
832+
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),
833833
};
834834

835835
let mut num_handles: usize = 0;

uefi/src/data_types/owned_strs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloc::string::String;
88
use alloc::vec;
99
use alloc::vec::Vec;
1010
use core::fmt::{self, Display, Formatter};
11-
use core::ops;
11+
use core::{ops, ptr};
1212

1313
/// Error returned by [`CString16::try_from::<&str>`].
1414
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -204,7 +204,7 @@ impl ops::Deref for CString16 {
204204
type Target = CStr16;
205205

206206
fn deref(&self) -> &CStr16 {
207-
unsafe { &*(self.0.as_slice() as *const [Char16] as *const CStr16) }
207+
unsafe { &*(ptr::from_ref(self.0.as_slice()) as *const CStr16) }
208208
}
209209
}
210210

uefi/src/data_types/strs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::borrow::Borrow;
55
use core::ffi::CStr;
66
use core::fmt::{self, Display, Formatter};
77
use core::mem::MaybeUninit;
8-
use core::slice;
8+
use core::{ptr, slice};
99

1010
#[cfg(feature = "alloc")]
1111
use super::CString16;
@@ -173,7 +173,7 @@ impl CStr8 {
173173
/// null-terminated string, with no interior null bytes.
174174
#[must_use]
175175
pub const unsafe fn from_bytes_with_nul_unchecked(chars: &[u8]) -> &Self {
176-
&*(chars as *const [u8] as *const Self)
176+
&*(ptr::from_ref(chars) as *const Self)
177177
}
178178

179179
/// Returns the inner pointer to this CStr8.
@@ -186,7 +186,7 @@ impl CStr8 {
186186
/// character.
187187
#[must_use]
188188
pub const fn as_bytes(&self) -> &[u8] {
189-
unsafe { &*(&self.0 as *const [Char8] as *const [u8]) }
189+
unsafe { &*(ptr::from_ref(&self.0) as *const [u8]) }
190190
}
191191
}
192192

@@ -407,7 +407,7 @@ impl CStr16 {
407407
/// null-terminated string, with no interior null characters.
408408
#[must_use]
409409
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
410-
&*(codes as *const [u16] as *const Self)
410+
&*(ptr::from_ref(codes) as *const Self)
411411
}
412412

413413
/// Creates a `&CStr16` from a [`Char16`] slice, stopping at the first nul character.
@@ -561,7 +561,7 @@ impl CStr16 {
561561
/// Converts this C string to a u16 slice containing the trailing null.
562562
#[must_use]
563563
pub const fn to_u16_slice_with_nul(&self) -> &[u16] {
564-
unsafe { &*(&self.0 as *const [Char16] as *const [u16]) }
564+
unsafe { &*(ptr::from_ref(&self.0) as *const [u16]) }
565565
}
566566

567567
/// Returns an iterator over this C string

uefi/src/fs/path/path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::fs::path::{PathBuf, SEPARATOR};
55
use crate::{CStr16, CString16};
66
use core::fmt::{Display, Formatter};
7+
use core::ptr;
78

89
/// A path similar to the `Path` of the standard library, but based on
910
/// [`CStr16`] strings and [`SEPARATOR`] as separator.
@@ -16,7 +17,7 @@ impl Path {
1617
/// Constructor.
1718
#[must_use]
1819
pub fn new<S: AsRef<CStr16> + ?Sized>(s: &S) -> &Self {
19-
unsafe { &*(s.as_ref() as *const CStr16 as *const Self) }
20+
unsafe { &*(ptr::from_ref(s.as_ref()) as *const Self) }
2021
}
2122

2223
/// Returns the underlying string.

uefi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
clippy::missing_const_for_fn,
221221
clippy::must_use_candidate,
222222
clippy::ptr_as_ptr,
223+
clippy::ref_as_ptr,
223224
clippy::use_self,
224225
missing_debug_implementations,
225226
missing_docs,

uefi/src/polyfill.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
//! behind unstable features.
33
44
use core::mem::MaybeUninit;
5+
use core::ptr;
56
#[cfg(feature = "alloc")]
67
use {alloc::vec::Vec, core::mem::ManuallyDrop};
78

89
/// Polyfill for the unstable `MaybeUninit::slice_assume_init_ref` function.
910
///
1011
/// See <https://github.com/rust-lang/rust/issues/63569>.
1112
pub const unsafe fn maybe_uninit_slice_assume_init_ref<T>(s: &[MaybeUninit<T>]) -> &[T] {
12-
unsafe { &*(s as *const [MaybeUninit<T>] as *const [T]) }
13+
unsafe { &*(ptr::from_ref(s) as *const [T]) }
1314
}
1415

1516
/// Polyfill for the unstable `MaybeUninit::slice_as_mut_ptr` function.

uefi/src/proto/console/gop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl GraphicsOutput {
134134
self.check_framebuffer_region((dest_x, dest_y), (width, height));
135135
(self.0.blt)(
136136
&mut self.0,
137-
&color as *const _ as *mut _,
137+
ptr::from_ref(&color) as *mut _,
138138
GraphicsOutputBltOperation::BLT_VIDEO_FILL,
139139
0,
140140
0,

uefi/src/proto/media/file/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub trait File: Sized {
169169
/// * [`uefi::Status::VOLUME_FULL`]
170170
/// * [`uefi::Status::BAD_BUFFER_SIZE`]
171171
fn set_info<Info: FileProtocolInfo + ?Sized>(&mut self, info: &Info) -> Result {
172-
let info_ptr = (info as *const Info).cast::<c_void>();
172+
let info_ptr = ptr::from_ref(info).cast::<c_void>();
173173
let info_size = mem::size_of_val(info);
174174
unsafe { (self.imp().set_info)(self.imp(), &Info::GUID, info_size, info_ptr).to_result() }
175175
}

uefi/src/proto/network/pxe.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl BaseCode {
173173
) -> Result<u64> {
174174
let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer {
175175
let buffer_size = u64::try_from(buffer.len()).unwrap();
176-
((&mut buffer[0] as *mut u8).cast(), buffer_size, false)
176+
(buffer.as_mut_ptr().cast(), buffer_size, false)
177177
} else {
178178
(null_mut(), 0, true)
179179
};
@@ -203,7 +203,7 @@ impl BaseCode {
203203
overwrite: bool,
204204
buffer: &[u8],
205205
) -> Result {
206-
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
206+
let buffer_ptr = buffer.as_ptr().cast_mut().cast();
207207
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
208208

209209
unsafe {
@@ -231,7 +231,7 @@ impl BaseCode {
231231
buffer: &'a mut [u8],
232232
) -> Result<impl Iterator<Item = core::result::Result<TftpFileInfo<'a>, ReadDirParseError>> + 'a>
233233
{
234-
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
234+
let buffer_ptr = buffer.as_mut_ptr().cast();
235235
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
236236

237237
let status = unsafe {
@@ -334,7 +334,7 @@ impl BaseCode {
334334
) -> Result<u64> {
335335
let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer {
336336
let buffer_size = u64::try_from(buffer.len()).unwrap();
337-
((&mut buffer[0] as *mut u8).cast(), buffer_size, false)
337+
(buffer.as_mut_ptr().cast(), buffer_size, false)
338338
} else {
339339
(null_mut(), 0, true)
340340
};
@@ -364,7 +364,7 @@ impl BaseCode {
364364
info: &MtftpInfo,
365365
) -> Result<impl Iterator<Item = core::result::Result<MtftpFileInfo<'a>, ReadDirParseError>> + 'a>
366366
{
367-
let buffer_ptr = (&buffer[0] as *const u8 as *mut u8).cast();
367+
let buffer_ptr = buffer.as_mut_ptr().cast();
368368
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
369369

370370
let status = unsafe {
@@ -464,7 +464,7 @@ impl BaseCode {
464464
let header_size_tmp;
465465
let (header_size, header_ptr) = if let Some(header) = header {
466466
header_size_tmp = header.len();
467-
(Some(&header_size_tmp), (&header[0] as *const u8).cast())
467+
(Some(&header_size_tmp), header.as_ptr().cast())
468468
} else {
469469
(None, null())
470470
};
@@ -481,7 +481,7 @@ impl BaseCode {
481481
header_size,
482482
header_ptr,
483483
&buffer.len(),
484-
(&buffer[0] as *const u8).cast(),
484+
buffer.as_ptr().cast(),
485485
)
486486
}
487487
.to_result()
@@ -502,7 +502,7 @@ impl BaseCode {
502502
let header_size_tmp;
503503
let (header_size, header_ptr) = if let Some(header) = header {
504504
header_size_tmp = header.len();
505-
(Some(&header_size_tmp), (&mut header[0] as *mut u8).cast())
505+
(Some(&header_size_tmp), header.as_mut_ptr().cast())
506506
} else {
507507
(None, null_mut())
508508
};
@@ -520,7 +520,7 @@ impl BaseCode {
520520
header_size,
521521
header_ptr,
522522
&mut buffer_size,
523-
(&mut buffer[0] as *mut u8).cast(),
523+
buffer.as_mut_ptr().cast(),
524524
)
525525
};
526526
status.to_result_with_val(|| buffer_size)

0 commit comments

Comments
 (0)