Skip to content

Commit 22ac6ed

Browse files
authored
Clippy Linting in CI (#124)
TL;DR ```toml complexity = "warn" correctness = "warn" module_inception = { level = "allow", priority = 1 } must_use_candidate = { level = "allow", priority = 1 } perf = "warn" style = "warn" suspicious = "warn" unreadable_literal = "warn" wildcard_imports = { level = "allow", priority = 1 } ```
1 parent 62105a3 commit 22ac6ed

File tree

14 files changed

+65
-40
lines changed

14 files changed

+65
-40
lines changed

.github/workflows/test.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ env:
77
RUST_BACKTRACE: 1
88

99
jobs:
10-
cargo-fmt:
10+
linting:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
1414
- uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0
1515
with:
1616
toolchain: "1.78"
17-
components: rustfmt
17+
components: rustfmt, clippy
1818
- name: Run rustfmt
1919
run: 'cargo fmt --all --check'
20+
- name: Run clippy
21+
run: 'cargo clippy --all-targets -- -D warnings'
2022

2123
windows-msvc:
2224
strategy:

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ license = "MIT/Apache-2.0"
1111
keywords = ["ffi", "libffi", "closure", "c"]
1212
edition = "2021"
1313
rust-version = "1.78"
14+
15+
[workspace.lints.clippy]
16+
complexity = "warn"
17+
correctness = "warn"
18+
module_inception = { level = "allow", priority = 1 }
19+
must_use_candidate = { level = "allow", priority = 1 }
20+
perf = "warn"
21+
style = "warn"
22+
suspicious = "warn"
23+
unreadable_literal = "warn"
24+
wildcard_imports = { level = "allow", priority = 1 }

libffi-rs/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ system = ["libffi-sys/system"]
2121

2222
[package.metadata.docs.rs]
2323
features = ["system"]
24+
25+
[lints]
26+
workspace = true

libffi-rs/examples/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use libffi::middle::Type;
22

33
fn main() {
4-
Type::structure(vec![Type::u16(), Type::u16()].into_iter());
4+
Type::structure(vec![Type::u16(), Type::u16()]);
55
}

libffi-rs/src/high/call.rs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ pub fn arg<T: super::CType>(arg: &T) -> Arg {
7474
///
7575
/// assert!((result - 5f32).abs() < 0.0001);
7676
/// ```
77+
/// # Safety
78+
/// The signature of the function pointer must match the types of the arguments and the return type.
79+
/// If the types do not match, we get UB.
7780
pub unsafe fn call<R: super::CType>(fun: CodePtr, args: &[Arg]) -> R {
7881
let types = args.iter().map(|arg| arg.type_.clone());
7982
let cif = middle::Cif::new(types, R::reify().into_middle());

libffi-rs/src/high/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl<T> Type<T> {
3636
///
3737
/// In particular, for any type `T` that implements `CType`, we can
3838
/// get a `Type<T>` for describing that type.
39+
/// # Safety
3940
/// This trait is unsafe to implement because if the libffi type
4041
/// associated with a Rust type doesn’t match then we get
4142
/// undefined behavior.

libffi-rs/src/low.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ pub unsafe fn prep_cif(
276276
/// - `abi` — the calling convention to use
277277
/// - `nfixedargs` — the number of fixed arguments
278278
/// - `ntotalargs` — the total number of arguments, including fixed and
279-
/// var args
279+
/// var args
280280
/// - `rtype` — the result type
281281
/// - `atypes` — the argument types (length must be at least `nargs`)
282282
///
@@ -308,7 +308,7 @@ pub unsafe fn prep_cif_var(
308308
/// # Arguments
309309
///
310310
/// * `cif` — describes the argument and result types and the calling
311-
/// convention
311+
/// convention
312312
/// * `fun` — the function to call
313313
/// * `args` — the arguments to pass to `fun`
314314
///
@@ -476,6 +476,9 @@ pub fn closure_alloc() -> (*mut ffi_closure, CodePtr) {
476476
/// closure_free(closure_handle);
477477
/// }
478478
/// ```
479+
/// # Safety
480+
/// The closure cannot be null and must be a valid pointer to a closure allocated with
481+
/// [`closure_alloc`].
479482
pub unsafe fn closure_free(closure: *mut ffi_closure) {
480483
raw::ffi_closure_free(closure as *mut c_void);
481484
}
@@ -530,7 +533,7 @@ pub type RawCallback = unsafe extern "C" fn(
530533
/// - `cif` — the calling convention and types for calling the closure
531534
/// - `callback` — the function that the closure will invoke
532535
/// - `userdata` — the closed-over value, stored in the closure and
533-
/// passed to the callback upon invocation
536+
/// passed to the callback upon invocation
534537
/// - `code` — the closure’s code pointer, *i.e.*, the second component
535538
/// returned by [`closure_alloc`].
536539
///
@@ -622,7 +625,7 @@ pub unsafe fn prep_closure<U, R>(
622625
/// - `cif` — the calling convention and types for calling the closure
623626
/// - `callback` — the function that the closure will invoke
624627
/// - `userdata` — the closed-over value, stored in the closure and
625-
/// passed to the callback upon invocation
628+
/// passed to the callback upon invocation
626629
/// - `code` — the closure’s code pointer, *i.e.*, the second component
627630
/// returned by [`closure_alloc`].
628631
///

libffi-rs/src/middle/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Cif {
115115
let args = args.into_iter();
116116
let nargs = args.len();
117117
let args = types::TypeArray::new(args);
118-
let mut cif: low::ffi_cif = Default::default();
118+
let mut cif = low::ffi_cif::default();
119119

120120
unsafe {
121121
low::prep_cif(
@@ -265,7 +265,7 @@ pub struct Closure<'a> {
265265
_marker: PhantomData<&'a ()>,
266266
}
267267

268-
impl<'a> Drop for Closure<'a> {
268+
impl Drop for Closure<'_> {
269269
fn drop(&mut self) {
270270
unsafe {
271271
low::closure_free(self.alloc);
@@ -464,7 +464,7 @@ mod test {
464464

465465
#[test]
466466
fn call() {
467-
let cif = Cif::new(vec![Type::i64(), Type::i64()].into_iter(), Type::i64());
467+
let cif = Cif::new(vec![Type::i64(), Type::i64()], Type::i64());
468468
let f = |m: i64, n: i64| -> i64 {
469469
unsafe { cif.call(CodePtr(add_it as *mut c_void), &[arg(&m), arg(&n)]) }
470470
};
@@ -480,7 +480,7 @@ mod test {
480480

481481
#[test]
482482
fn closure() {
483-
let cif = Cif::new(vec![Type::u64()].into_iter(), Type::u64());
483+
let cif = Cif::new(vec![Type::u64()], Type::u64());
484484
let env: u64 = 5;
485485
let closure = Closure::new(cif, callback, &env);
486486

@@ -502,7 +502,7 @@ mod test {
502502

503503
#[test]
504504
fn rust_lambda() {
505-
let cif = Cif::new(vec![Type::u64(), Type::u64()].into_iter(), Type::u64());
505+
let cif = Cif::new(vec![Type::u64(), Type::u64()], Type::u64());
506506
let env = |x: u64, y: u64| x + y;
507507
let closure = Closure::new(cif, callback2, &env);
508508

@@ -534,8 +534,7 @@ mod test {
534534
Type::i64(),
535535
]),
536536
Type::u64(),
537-
]
538-
.into_iter(),
537+
],
539538
Type::u64(),
540539
);
541540
let clone_cif = cif.clone();

libffi-rs/src/middle/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ unsafe fn ffi_type_clone(old: Type_) -> Owned<Type_> {
156156
size,
157157
..
158158
} = *old;
159+
// Create new
159160
ffi_type_struct_create_raw(ffi_type_array_clone(elements), size, alignment)
160161
} else {
161162
old

libffi-sys-rs/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ features = ["system"]
2121

2222
[build-dependencies]
2323
cc = "1.0"
24+
25+
[lints]
26+
workspace = true

libffi-sys-rs/build/common.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ pub use std::{
77
#[track_caller]
88
pub fn run_command(which: &'static str, cmd: &mut Command) {
99
match cmd.status() {
10-
Ok(status) if status.success() => return,
10+
Ok(status) if status.success() => (),
1111
Ok(status) => {
12-
println!("cargo:warning={} failed with {}", which, status);
13-
panic!("{}: {} ({:?})", which, status, cmd);
12+
println!("cargo:warning={which} failed with {status}");
13+
panic!("{which}: {status} ({cmd:?})");
1414
}
1515
Err(err) => {
16-
println!("cargo:warning={} failed with error {}", which, err);
17-
panic!("{}: {} ({:?})", which, err, cmd);
16+
println!("cargo:warning={which} failed with error {err}");
17+
panic!("{which}: {err} ({cmd:?})");
1818
}
1919
}
2020
}

libffi-sys-rs/build/msvc.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const BUILD_FILES_X86_64: &[&str] = &["x86/ffi.c", "x86/ffiw64.c"];
1919
const BUILD_FILES_AARCH64: &[&str] = &["aarch64/ffi.c"];
2020

2121
fn add_file(build: &mut cc::Build, file: &str) {
22-
build.file(format!("libffi/src/{}", file));
22+
build.file(format!("libffi/src/{file}"));
2323
}
2424

2525
fn unsupported(arch: &str) -> ! {
26-
panic!("Unsupported architecture: {}", arch)
26+
panic!("Unsupported architecture: {arch}")
2727
}
2828

2929
pub fn build_and_link() {
@@ -83,8 +83,7 @@ pub fn probe_and_link() {
8383

8484
pub fn pre_process_asm(include_dirs: &[&str], target: &str, target_arch: &str) -> String {
8585
let folder_name = match target_arch {
86-
"x86" => "x86",
87-
"x86_64" => "x86",
86+
"x86" | "x86_64" => "x86",
8887
"aarch64" => "aarch64",
8988
_ => unsupported(target_arch),
9089
};
@@ -111,9 +110,9 @@ pub fn pre_process_asm(include_dirs: &[&str], target: &str, target_arch: &str) -
111110
}
112111

113112
cmd.arg("/EP");
114-
cmd.arg(format!("libffi/src/{}/{}.S", folder_name, file_name));
113+
cmd.arg(format!("libffi/src/{folder_name}/{file_name}.S"));
115114

116-
let out_path = format!("libffi/src/{}/{}.asm", folder_name, file_name);
115+
let out_path = format!("libffi/src/{folder_name}/{file_name}.asm");
117116
let asm_file = fs::File::create(&out_path).expect("Could not create output file");
118117

119118
cmd.stdout(asm_file);

libffi-sys-rs/src/arch.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ mod powerpc {
157157
use crate::ffi_abi;
158158

159159
pub const ffi_abi_FFI_FIRST_ABI: ffi_abi = 0;
160-
pub const ffi_abi_FFI_SYSV_SOFT_FLOAT: ffi_abi = 0b000001;
161-
pub const ffi_abi_FFI_SYSV_STRUCT_RET: ffi_abi = 0b000010;
162-
pub const ffi_abi_FFI_SYSV_IBM_LONG_DOUBLE: ffi_abi = 0b000100;
163-
pub const ffi_abi_FFI_SYSV: ffi_abi = 0b001000;
164-
pub const ffi_abi_FFI_SYSV_LONG_DOUBLE_128: ffi_abi = 0b010000;
160+
pub const ffi_abi_FFI_SYSV_SOFT_FLOAT: ffi_abi = 0b00_0001;
161+
pub const ffi_abi_FFI_SYSV_STRUCT_RET: ffi_abi = 0b00_0010;
162+
pub const ffi_abi_FFI_SYSV_IBM_LONG_DOUBLE: ffi_abi = 0b00_0100;
163+
pub const ffi_abi_FFI_SYSV: ffi_abi = 0b00_1000;
164+
pub const ffi_abi_FFI_SYSV_LONG_DOUBLE_128: ffi_abi = 0b01_0000;
165165

166166
mod fprs {
167167
pub const SOFT_FLOAT_FLAG: crate::ffi_abi = 0b0;
@@ -224,10 +224,10 @@ mod powerpc {
224224
use crate::ffi_abi;
225225

226226
pub const ffi_abi_FFI_FIRST_ABI: ffi_abi = 0;
227-
pub const ffi_abi_FFI_LINUX_STRUCT_ALIGN: ffi_abi = 0b000001;
228-
pub const ffi_abi_FFI_LINUX_LONG_DOUBLE_128: ffi_abi = 0b000010;
229-
pub const ffi_abi_FFI_LINUX_LONG_DOUBLE_IEEE128: ffi_abi = 0b000100;
230-
pub const ffi_abi_FFI_LINUX: ffi_abi = 0b001000;
227+
pub const ffi_abi_FFI_LINUX_STRUCT_ALIGN: ffi_abi = 0b00_0001;
228+
pub const ffi_abi_FFI_LINUX_LONG_DOUBLE_128: ffi_abi = 0b00_0010;
229+
pub const ffi_abi_FFI_LINUX_LONG_DOUBLE_IEEE128: ffi_abi = 0b00_0100;
230+
pub const ffi_abi_FFI_LINUX: ffi_abi = 0b00_1000;
231231

232232
mod elfv1 {
233233
pub const STRUCT_ALIGN_FLAG: crate::ffi_abi = 0b0;

libffi-sys-rs/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub type ffi_abi = u32;
5858
pub type ffi_status = u32;
5959
pub type ffi_type_enum = u32;
6060

61-
pub const FFI_64_BIT_MAX: u64 = 9223372036854775807;
61+
pub const FFI_64_BIT_MAX: u64 = 9_223_372_036_854_775_807;
6262
pub const FFI_CLOSURES: u32 = 1;
6363
pub const FFI_SIZEOF_ARG: usize = core::mem::size_of::<c_long>();
6464
// NOTE: This only differs from FFI_SIZEOF_ARG on ILP platforms, which Rust does not support
@@ -192,7 +192,7 @@ pub struct ffi_closure {
192192
pub user_data: *mut c_void,
193193
}
194194

195-
/// Implements Debug manually since sometimes FFI_TRAMPOLINE_SIZE is too large
195+
/// Implements Debug manually since sometimes `FFI_TRAMPOLINE_SIZE` is too large
196196
impl Debug for ffi_closure {
197197
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
198198
f.debug_struct("ffi_closure")
@@ -238,7 +238,7 @@ pub struct ffi_raw_closure {
238238
pub user_data: *mut c_void,
239239
}
240240

241-
/// Implements Debug manually since sometimes FFI_TRAMPOLINE_SIZE is too large
241+
/// Implements Debug manually since sometimes `FFI_TRAMPOLINE_SIZE` is too large
242242
impl Debug for ffi_raw_closure {
243243
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
244244
let mut debug_struct = f.debug_struct("ffi_raw_closure");
@@ -291,7 +291,7 @@ pub struct ffi_java_raw_closure {
291291
pub user_data: *mut c_void,
292292
}
293293

294-
/// Implements Debug manually since sometimes FFI_TRAMPOLINE_SIZE is too large
294+
/// Implements Debug manually since sometimes `FFI_TRAMPOLINE_SIZE` is too large
295295
impl Debug for ffi_java_raw_closure {
296296
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
297297
let mut debug_struct = f.debug_struct("ffi_java_raw_closure");
@@ -562,13 +562,13 @@ mod test {
562562
fn test_function_sign_extension() {
563563
unsafe {
564564
let mut cif: ffi_cif = Default::default();
565-
let mut arg_types: Vec<*mut ffi_type> = vec![&mut ffi_type_uint8];
565+
let mut arg_types: Vec<*mut ffi_type> = vec![addr_of_mut!(ffi_type_uint8)];
566566

567567
let prep_status = ffi_prep_cif(
568568
&mut cif,
569569
ffi_abi_FFI_DEFAULT_ABI,
570570
1,
571-
&mut ffi_type_uint32,
571+
addr_of_mut!(ffi_type_uint8),
572572
arg_types.as_mut_ptr(),
573573
);
574574

0 commit comments

Comments
 (0)