Skip to content

Commit 9269581

Browse files
committed
clippy: prefer const fn where possible
This has the benefit that users clearly see that no side effects can happen. However, the public API changes in the sense that once we remove const for a function, user code might break. Nevertheless, this is a code improvement, IMHO. Regarding the device path code, which is generated: So far the simplest solution is to deactivate this lint there. It is not trivial to decide which function can be const and which not during code generation. It is future work to solve this, if necessary.
1 parent 9735b7c commit 9269581

File tree

24 files changed

+84
-78
lines changed

24 files changed

+84
-78
lines changed

uefi/src/data_types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Handle {
3939

4040
/// Get the underlying raw pointer.
4141
#[must_use]
42-
pub fn as_ptr(&self) -> *mut c_void {
42+
pub const fn as_ptr(&self) -> *mut c_void {
4343
self.0.as_ptr()
4444
}
4545

@@ -78,7 +78,7 @@ impl Event {
7878

7979
/// Get the underlying raw pointer.
8080
#[must_use]
81-
pub fn as_ptr(&self) -> *mut c_void {
81+
pub const fn as_ptr(&self) -> *mut c_void {
8282
self.0.as_ptr()
8383
}
8484
}

uefi/src/data_types/owned_strs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl CString16 {
113113

114114
impl Default for CString16 {
115115
fn default() -> Self {
116-
CString16::new()
116+
Self::new()
117117
}
118118
}
119119

@@ -141,7 +141,7 @@ impl TryFrom<&str> for CString16 {
141141
// Add trailing nul.
142142
output.push(NUL_16);
143143

144-
Ok(CString16(output))
144+
Ok(Self(output))
145145
}
146146
}
147147

@@ -175,7 +175,7 @@ impl<'a> TryFrom<&UnalignedSlice<'a, u16>> for CString16 {
175175

176176
fn try_from(input: &UnalignedSlice<u16>) -> Result<Self, Self::Error> {
177177
let v = input.to_vec();
178-
CString16::try_from(v)
178+
Self::try_from(v)
179179
}
180180
}
181181

@@ -189,7 +189,7 @@ impl From<&CStr16> for CString16 {
189189
impl From<&CString16> for String {
190190
fn from(value: &CString16) -> Self {
191191
let slice: &CStr16 = value.as_ref();
192-
String::from(slice)
192+
Self::from(slice)
193193
}
194194
}
195195

uefi/src/data_types/strs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl CStr16 {
524524

525525
/// Returns if the string is empty. This ignores the null character.
526526
#[must_use]
527-
pub fn is_empty(&self) -> bool {
527+
pub const fn is_empty(&self) -> bool {
528528
self.num_chars() == 0
529529
}
530530

@@ -566,7 +566,7 @@ impl CStr16 {
566566
/// Returns the underlying bytes as slice including the terminating null
567567
/// character.
568568
#[must_use]
569-
pub fn as_bytes(&self) -> &[u8] {
569+
pub const fn as_bytes(&self) -> &[u8] {
570570
unsafe { slice::from_raw_parts(self.0.as_ptr().cast(), self.num_bytes()) }
571571
}
572572
}
@@ -593,7 +593,7 @@ impl From<&CStr16> for alloc::string::String {
593593
.map(u16::from)
594594
.map(u32::from)
595595
.map(|int| char::from_u32(int).expect("Should be encodable as UTF-8"))
596-
.collect::<alloc::string::String>()
596+
.collect::<Self>()
597597
}
598598
}
599599

uefi/src/data_types/unaligned_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
2828
/// The `data` pointer must point to a packed array of at least
2929
/// `len` elements of type `T`. The pointer must remain valid for as
3030
/// long as the `'a` lifetime.
31-
pub unsafe fn new(data: *const T, len: usize) -> Self {
31+
pub const unsafe fn new(data: *const T, len: usize) -> Self {
3232
Self {
3333
data,
3434
len,

uefi/src/fs/dir_entry_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct UefiDirectoryIter(UefiDirectoryHandle);
1818
impl UefiDirectoryIter {
1919
/// Constructor.
2020
#[must_use]
21-
pub fn new(handle: UefiDirectoryHandle) -> Self {
21+
pub const fn new(handle: UefiDirectoryHandle) -> Self {
2222
Self(handle)
2323
}
2424
}

uefi/src/fs/file_system/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct FileSystem<'a>(ScopedProtocol<'a, SimpleFileSystemProtocol>);
2626
impl<'a> FileSystem<'a> {
2727
/// Constructor.
2828
#[must_use]
29-
pub fn new(proto: ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
29+
pub const fn new(proto: ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
3030
Self(proto)
3131
}
3232

uefi/src/fs/path/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Path {
2121

2222
/// Returns the underlying string.
2323
#[must_use]
24-
pub fn to_cstr16(&self) -> &CStr16 {
24+
pub const fn to_cstr16(&self) -> &CStr16 {
2525
&self.0
2626
}
2727

@@ -79,7 +79,7 @@ impl Path {
7979

8080
/// Returns of the path is empty.
8181
#[must_use]
82-
pub fn is_empty(&self) -> bool {
82+
pub const fn is_empty(&self) -> bool {
8383
self.to_cstr16().is_empty()
8484
}
8585
}

uefi/src/helpers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn system_table() -> SystemTable<Boot> {
5454
/// **PLEASE NOTE** that these helpers are meant for the pre exit boot service
5555
/// epoch. Limited functionality might work after exiting them, such as logging
5656
/// to the debugcon device.
57+
#[allow(clippy::missing_const_for_fn)]
5758
pub fn init() -> Result<()> {
5859
// Setup logging and memory allocation
5960

@@ -72,6 +73,7 @@ pub fn init() -> Result<()> {
7273
Ok(())
7374
}
7475

76+
#[allow(clippy::missing_const_for_fn)]
7577
pub(crate) fn exit() {
7678
#[cfg(feature = "logger")]
7779
logger::disable();

uefi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
clippy::all,
9494
clippy::must_use_candidate,
9595
clippy::use_self
96+
clippy::use_self,
97+
clippy::missing_const_for_fn
9698
)]
9799
#![deny(missing_debug_implementations)]
98100

uefi/src/mem/memory_map/impl_.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl MemoryMapBackingMemory {
254254
/// takes into account that, as you go, more (small) allocations might
255255
/// happen.
256256
#[must_use]
257-
fn safe_allocation_size_hint(mmm: MemoryMapMeta) -> usize {
257+
const fn safe_allocation_size_hint(mmm: MemoryMapMeta) -> usize {
258258
// Allocate space for extra entries beyond the current size of the
259259
// memory map. The value of 8 matches the value in the Linux kernel:
260260
// https://github.com/torvalds/linux/blob/e544a07438/drivers/firmware/efi/libstub/efistub.h#L173
@@ -308,7 +308,7 @@ impl MemoryMapOwned {
308308
pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self {
309309
assert!(meta.desc_size >= mem::size_of::<MemoryDescriptor>());
310310
let len = meta.entry_count();
311-
MemoryMapOwned {
311+
Self {
312312
key: MemoryMapKey(0),
313313
buf,
314314
meta,

0 commit comments

Comments
 (0)