From c30c5b0a0b317f05326a2fdb2cade474a03cdcae Mon Sep 17 00:00:00 2001 From: MaxXSoft Date: Sat, 30 Nov 2024 14:54:48 +0800 Subject: [PATCH] (src) removed some unnecessary `allow(clippy::...)` attributes --- crates/libkoopa/src/driver.rs | 27 +++++++++++++++++++++------ crates/libkoopa/src/raw/builder.rs | 1 - crates/libkoopa/src/raw/ffi.rs | 9 +++++++-- crates/libkoopa/src/utils.rs | 25 +++++++++++++++++++------ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/crates/libkoopa/src/driver.rs b/crates/libkoopa/src/driver.rs index c230dae..36d20a1 100644 --- a/crates/libkoopa/src/driver.rs +++ b/crates/libkoopa/src/driver.rs @@ -12,8 +12,13 @@ ffi! { /// Updates the `program` if no errors occurred. /// /// Returns the error code. - fn koopa_parse_from_file(path: *const c_char, program: &mut *mut Program) -> ErrorCode { - let path = unwrap_or_return!(unsafe { CStr::from_ptr(path) }.to_str(), InvalidUtf8String); + /// + /// # Safety + /// + /// The memory pointed to by `path` must contain a valid + /// null terminator at the end of the string. + unsafe fn koopa_parse_from_file(path: *const c_char, program: &mut *mut Program) -> ErrorCode { + let path = unwrap_or_return!(CStr::from_ptr(path).to_str(), InvalidUtf8String); let driver = unwrap_or_return!(Driver::from_path(path), InvalidFile); let prog = unwrap_or_return!(driver.generate_program(), InvalidKoopaProgram); *program = new_pointer(prog); @@ -24,8 +29,13 @@ ffi! { /// Updates the `program` if no errors occurred. /// /// Returns the error code. - fn koopa_parse_from_string(s: *const c_char, program: &mut *mut Program) -> ErrorCode { - let s = unwrap_or_return!(unsafe { CStr::from_ptr(s) }.to_str(), InvalidUtf8String); + /// + /// # Safety + /// + /// The memory pointed to by `s` must contain a valid + /// null terminator at the end of the string. + unsafe fn koopa_parse_from_string(s: *const c_char, program: &mut *mut Program) -> ErrorCode { + let s = unwrap_or_return!(CStr::from_ptr(s).to_str(), InvalidUtf8String); let driver = Driver::from(s); let prog = unwrap_or_return!(driver.generate_program(), InvalidKoopaProgram); *program = new_pointer(prog); @@ -60,7 +70,12 @@ ffi! { /// /// All programs returned by Koopa IR library functions /// should be deleted manually. - fn koopa_delete_program(program: *mut Program) { - unsafe { drop_pointer(program) }; + /// + /// # Safety + /// + /// The `program` must be a valid program pointer returned by + /// Koopa IR library functions. + unsafe fn koopa_delete_program(program: *mut Program) { + drop_pointer(program); } } diff --git a/crates/libkoopa/src/raw/builder.rs b/crates/libkoopa/src/raw/builder.rs index 378179a..47ab893 100644 --- a/crates/libkoopa/src/raw/builder.rs +++ b/crates/libkoopa/src/raw/builder.rs @@ -28,7 +28,6 @@ impl RawProgramBuilder { } /// Builds on the given Koopa IR program. - #[allow(clippy::needless_collect)] pub fn build_on(&mut self, program: &Program) -> RawProgram { let mut info = ProgramInfo::new(program); let raw = RawProgram { diff --git a/crates/libkoopa/src/raw/ffi.rs b/crates/libkoopa/src/raw/ffi.rs index d007f5b..423c857 100644 --- a/crates/libkoopa/src/raw/ffi.rs +++ b/crates/libkoopa/src/raw/ffi.rs @@ -10,8 +10,13 @@ ffi! { } /// Frees allocated memory of the given raw program builder. - fn koopa_delete_raw_program_builder(builder: *mut RawProgramBuilder) { - unsafe { drop_pointer(builder) }; + /// + /// # Safety + /// + /// The `builder` must be a valid raw program builder returned by + /// Koopa IR library functions. + unsafe fn koopa_delete_raw_program_builder(builder: *mut RawProgramBuilder) { + drop_pointer(builder); } /// Builds a raw program of the given Koopa IR program diff --git a/crates/libkoopa/src/utils.rs b/crates/libkoopa/src/utils.rs index bfcf2c8..a850d65 100644 --- a/crates/libkoopa/src/utils.rs +++ b/crates/libkoopa/src/utils.rs @@ -27,14 +27,27 @@ pub(crate) unsafe fn new_uninit_box() -> Box { /// Defines FFI functions. macro_rules! ffi { { - $($(#[$attr:meta])* + $(#[$attr:meta])* fn $name:ident$(<$($lt:lifetime)+>)? - ($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)? $body:block)* - } => { - $($(#[$attr])* - #[allow(clippy::not_unsafe_ptr_arg_deref)] + ($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)? $body:block + $($rest:tt)* + } => { + $(#[$attr])* #[no_mangle] - pub extern "C" fn $name$(<$($lt)+>)?($($arg: $ty),*) $(-> $ret)? $body)* + pub extern "C" fn $name$(<$($lt)+>)? ($($arg: $ty),*) $(-> $ret)? $body + $crate::utils::ffi!($($rest)*); }; + { + $(#[$attr:meta])* + unsafe fn $name:ident$(<$($lt:lifetime)+>)? + ($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)? $body:block + $($rest:tt)* + } => { + $(#[$attr])* + #[no_mangle] + pub unsafe extern "C" fn $name$(<$($lt)+>)? ($($arg: $ty),*) $(-> $ret)? $body + $crate::utils::ffi!($($rest)*); + }; + () => {}; } pub(crate) use ffi;