Skip to content

clippy: add use_self and const fn #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions uefi-raw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@

#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(missing_debug_implementations)]
#![deny(clippy::all)]
#![deny(clippy::ptr_as_ptr, unused)]
#![deny(clippy::must_use_candidate)]
#![deny(
clippy::all,
clippy::missing_const_for_fn,
clippy::must_use_candidate,
clippy::ptr_as_ptr,
clippy::use_self,
missing_debug_implementations,
unused
)]

#[macro_use]
mod enums;
Expand Down
4 changes: 2 additions & 2 deletions uefi-raw/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ impl Status {
#[inline]
#[must_use]
pub fn is_success(self) -> bool {
self == Status::SUCCESS
self == Self::SUCCESS
}

/// Returns true if status code indicates a warning.
#[inline]
#[must_use]
pub fn is_warning(self) -> bool {
(self != Status::SUCCESS) && (self.0 & Self::ERROR_BIT == 0)
(self != Self::SUCCESS) && (self.0 & Self::ERROR_BIT == 0)
}

/// Returns true if the status code indicates an error.
Expand Down
8 changes: 4 additions & 4 deletions uefi-raw/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ impl MemoryDescriptor {
}

impl Default for MemoryDescriptor {
fn default() -> MemoryDescriptor {
MemoryDescriptor {
fn default() -> Self {
Self {
ty: MemoryType::RESERVED,
phys_start: 0,
virt_start: 0,
Expand Down Expand Up @@ -439,9 +439,9 @@ impl MemoryType {
/// Construct a custom `MemoryType`. Values in the range `0x8000_0000..=0xffff_ffff` are free for use if you are
/// an OS loader.
#[must_use]
pub const fn custom(value: u32) -> MemoryType {
pub const fn custom(value: u32) -> Self {
assert!(value >= 0x80000000);
MemoryType(value)
Self(value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion uefi-raw/src/table/revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Revision {
let major = major as u32;
let minor = minor as u32;
let value = (major << 16) | minor;
Revision(value)
Self(value)
}

/// Returns the major revision.
Expand Down
2 changes: 1 addition & 1 deletion uefi-raw/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Display for Time {

/// The padding fields of `Time` are ignored for comparison.
impl PartialEq for Time {
fn eq(&self, other: &Time) -> bool {
fn eq(&self, other: &Self) -> bool {
self.year == other.year
&& self.month == other.month
&& self.day == other.day
Expand Down
12 changes: 6 additions & 6 deletions uefi/src/data_types/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ impl TryFrom<char> for Char8 {
}

impl From<Char8> for char {
fn from(char: Char8) -> char {
char::from(char.0)
fn from(char: Char8) -> Self {
Self::from(char.0)
}
}

impl From<u8> for Char8 {
fn from(value: u8) -> Self {
Char8(value)
Self(value)
}
}

impl From<Char8> for u8 {
fn from(char: Char8) -> u8 {
fn from(char: Char8) -> Self {
char.0
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl TryFrom<char> for Char16 {
}

impl From<Char16> for char {
fn from(char: Char16) -> char {
fn from(char: Char16) -> Self {
u32::from(char.0).try_into().unwrap()
}
}
Expand All @@ -127,7 +127,7 @@ impl TryFrom<u16> for Char16 {
}

impl From<Char16> for u16 {
fn from(char: Char16) -> u16 {
fn from(char: Char16) -> Self {
char.0
}
}
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Handle {

/// Get the underlying raw pointer.
#[must_use]
pub fn as_ptr(&self) -> *mut c_void {
pub const fn as_ptr(&self) -> *mut c_void {
self.0.as_ptr()
}

Expand Down Expand Up @@ -78,7 +78,7 @@ impl Event {

/// Get the underlying raw pointer.
#[must_use]
pub fn as_ptr(&self) -> *mut c_void {
pub const fn as_ptr(&self) -> *mut c_void {
self.0.as_ptr()
}
}
Expand Down
8 changes: 4 additions & 4 deletions uefi/src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl CString16 {

impl Default for CString16 {
fn default() -> Self {
CString16::new()
Self::new()
}
}

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

Ok(CString16(output))
Ok(Self(output))
}
}

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

fn try_from(input: &UnalignedSlice<u16>) -> Result<Self, Self::Error> {
let v = input.to_vec();
CString16::try_from(v)
Self::try_from(v)
}
}

Expand All @@ -189,7 +189,7 @@ impl From<&CStr16> for CString16 {
impl From<&CString16> for String {
fn from(value: &CString16) -> Self {
let slice: &CStr16 = value.as_ref();
String::from(slice)
Self::from(slice)
}
}

Expand Down
14 changes: 7 additions & 7 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl CStr16 {
pub fn from_unaligned_slice<'buf>(
src: &UnalignedSlice<'_, u16>,
buf: &'buf mut [MaybeUninit<u16>],
) -> Result<&'buf CStr16, UnalignedCStr16Error> {
) -> Result<&'buf Self, UnalignedCStr16Error> {
// The input `buf` might be longer than needed, so get a
// subslice of the required length.
let buf = buf
Expand All @@ -469,7 +469,7 @@ impl CStr16 {
// Safety: `copy_buf` fully initializes the slice.
maybe_uninit_slice_assume_init_ref(buf)
};
CStr16::from_u16_with_nul(buf).map_err(|e| match e {
Self::from_u16_with_nul(buf).map_err(|e| match e {
FromSliceWithNulError::InvalidChar(v) => UnalignedCStr16Error::InvalidChar(v),
FromSliceWithNulError::InteriorNul(v) => UnalignedCStr16Error::InteriorNul(v),
FromSliceWithNulError::NotNulTerminated => UnalignedCStr16Error::NotNulTerminated,
Expand Down Expand Up @@ -524,7 +524,7 @@ impl CStr16 {

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

Expand Down Expand Up @@ -566,7 +566,7 @@ impl CStr16 {
/// Returns the underlying bytes as slice including the terminating null
/// character.
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
pub const fn as_bytes(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.0.as_ptr().cast(), self.num_bytes()) }
}
}
Expand All @@ -593,7 +593,7 @@ impl From<&CStr16> for alloc::string::String {
.map(u16::from)
.map(u32::from)
.map(|int| char::from_u32(int).expect("Should be encodable as UTF-8"))
.collect::<alloc::string::String>()
.collect::<Self>()
}
}

Expand All @@ -615,8 +615,8 @@ impl<StrType: AsRef<str> + ?Sized> EqStrUntilNul<StrType> for CStr16 {
}
}

impl AsRef<CStr16> for CStr16 {
fn as_ref(&self) -> &CStr16 {
impl AsRef<Self> for CStr16 {
fn as_ref(&self) -> &Self {
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/data_types/unaligned_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a, T: Copy> UnalignedSlice<'a, T> {
/// The `data` pointer must point to a packed array of at least
/// `len` elements of type `T`. The pointer must remain valid for as
/// long as the `'a` lifetime.
pub unsafe fn new(data: *const T, len: usize) -> Self {
pub const unsafe fn new(data: *const T, len: usize) -> Self {
Self {
data,
len,
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/fs/dir_entry_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct UefiDirectoryIter(UefiDirectoryHandle);
impl UefiDirectoryIter {
/// Constructor.
#[must_use]
pub fn new(handle: UefiDirectoryHandle) -> Self {
pub const fn new(handle: UefiDirectoryHandle) -> Self {
Self(handle)
}
}
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/fs/file_system/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct FileSystem<'a>(ScopedProtocol<'a, SimpleFileSystemProtocol>);
impl<'a> FileSystem<'a> {
/// Constructor.
#[must_use]
pub fn new(proto: ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
pub const fn new(proto: ScopedProtocol<'a, SimpleFileSystemProtocol>) -> Self {
Self(proto)
}

Expand Down
4 changes: 2 additions & 2 deletions uefi/src/fs/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Path {

/// Returns the underlying string.
#[must_use]
pub fn to_cstr16(&self) -> &CStr16 {
pub const fn to_cstr16(&self) -> &CStr16 {
&self.0
}

Expand Down Expand Up @@ -79,7 +79,7 @@ impl Path {

/// Returns of the path is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.to_cstr16().is_empty()
}
}
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/helpers/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl core::fmt::Write for DebugconWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
for &byte in s.as_bytes() {
unsafe {
core::arch::asm!("outb %al, %dx", in("al") byte, in("dx") DebugconWriter::IO_PORT, options(att_syntax))
core::arch::asm!("outb %al, %dx", in("al") byte, in("dx") Self::IO_PORT, options(att_syntax))
};
}
Ok(())
Expand All @@ -83,7 +83,7 @@ impl Logger {
/// [`set_output`]: Self::set_output
#[must_use]
pub const fn new() -> Self {
Logger {
Self {
writer: AtomicPtr::new(ptr::null_mut()),
}
}
Expand Down
2 changes: 2 additions & 0 deletions uefi/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn system_table() -> SystemTable<Boot> {
/// **PLEASE NOTE** that these helpers are meant for the pre exit boot service
/// epoch. Limited functionality might work after exiting them, such as logging
/// to the debugcon device.
#[allow(clippy::missing_const_for_fn)]
pub fn init() -> Result<()> {
// Setup logging and memory allocation

Expand All @@ -72,6 +73,7 @@ pub fn init() -> Result<()> {
Ok(())
}

#[allow(clippy::missing_const_for_fn)]
pub(crate) fn exit() {
#[cfg(feature = "logger")]
logger::disable();
Expand Down
8 changes: 6 additions & 2 deletions uefi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@
#![no_std]
// Enable some additional warnings and lints.
#![warn(clippy::ptr_as_ptr, missing_docs, unused)]
#![deny(clippy::all)]
#![deny(clippy::must_use_candidate)]
#![deny(
clippy::all,
clippy::must_use_candidate,
clippy::use_self,
clippy::missing_const_for_fn
)]
#![deny(missing_debug_implementations)]

#[cfg(feature = "alloc")]
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/mem/memory_map/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl MemoryMapBackingMemory {
/// takes into account that, as you go, more (small) allocations might
/// happen.
#[must_use]
fn safe_allocation_size_hint(mmm: MemoryMapMeta) -> usize {
const fn safe_allocation_size_hint(mmm: MemoryMapMeta) -> usize {
// Allocate space for extra entries beyond the current size of the
// memory map. The value of 8 matches the value in the Linux kernel:
// https://github.com/torvalds/linux/blob/e544a07438/drivers/firmware/efi/libstub/efistub.h#L173
Expand Down Expand Up @@ -373,7 +373,7 @@ impl MemoryMapOwned {
pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self {
assert!(meta.desc_size >= mem::size_of::<MemoryDescriptor>());
let len = meta.entry_count();
MemoryMapOwned { buf, meta, len }
Self { buf, meta, len }
}
}

Expand Down
2 changes: 1 addition & 1 deletion uefi/src/proto/console/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl GraphicsOutput {

/// Returns a [`ModeIter`].
#[must_use]
pub fn modes<'a>(&'a self, bs: &'a BootServices) -> ModeIter {
pub const fn modes<'a>(&'a self, bs: &'a BootServices) -> ModeIter {
ModeIter {
gop: self,
bs,
Expand Down
6 changes: 3 additions & 3 deletions uefi/src/proto/console/text/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ pub enum Key {
}

impl From<InputKey> for Key {
fn from(k: InputKey) -> Key {
fn from(k: InputKey) -> Self {
if k.scan_code == ScanCode::NULL.0 {
Key::Printable(Char16::try_from(k.unicode_char).unwrap())
Self::Printable(Char16::try_from(k.unicode_char).unwrap())
} else {
Key::Special(ScanCode(k.scan_code))
Self::Special(ScanCode(k.scan_code))
}
}
}
Expand Down
Loading