From d29d3dfd11acaabfca7c0ebd9f58a9b13f84ae7a Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Mon, 5 Jun 2023 01:49:47 +0200 Subject: [PATCH] rust: enable `no_mangle_with_rust_abi` Clippy lint Introduced in Rust 1.69.0 [1], this lint prevents forgetting to set the C ABI when using `#[no_mangle]` (or thinking it is implied). For instance, it would have prevented the issue [2] fixed by commit c682e4c37d2b ("rust: kernel: Mark rust_fmt_argument as extern "C""). error: `#[no_mangle]` set on a function with the default (`Rust`) ABI --> rust/kernel/print.rs:21:1 | 21 | / unsafe fn rust_fmt_argument( 22 | | buf: *mut c_char, 23 | | end: *mut c_char, 24 | | ptr: *const c_void, 25 | | ) -> *mut c_char { | |________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#no_mangle_with_rust_abi = note: requested on the command line with `-D clippy::no-mangle-with-rust-abi` help: set an ABI | 21 | unsafe extern "C" fn rust_fmt_argument( | ++++++++++ help: or explicitly set the default | 21 | unsafe extern "Rust" fn rust_fmt_argument( | +++++++++++++ Thus enable it. In some cases, we need to use the Rust ABI even with `#[no_mangle]`, and for that, one may use `extern "Rust"` explicitly, but `rustfmt` overwrites it (and there does not seem to be an option to prevent that: `force_explicit_abi` does not allow to control that part, and even if it did, or if there was another option, we may not use it, since so far we have been using the defaults). Therefore, use `allow`s instead for the few cases we had. Link: https://github.com/rust-lang/rust-clippy/issues/10347 [1] Link: https://github.com/Rust-for-Linux/linux/pull/967 [2] Link: https://rust-lang.github.io/rustfmt/?version=v1.5.2&search=force_explicit_abi#force_explicit_abi [3] Signed-off-by: Miguel Ojeda --- Makefile | 1 + rust/kernel/allocator.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Makefile b/Makefile index f5543eef4f8227..c33d8533c46d35 100644 --- a/Makefile +++ b/Makefile @@ -466,6 +466,7 @@ export rust_common_flags := --edition=2021 \ -Dclippy::let_unit_value -Dclippy::mut_mut \ -Dclippy::needless_bitwise_bool \ -Dclippy::needless_continue \ + -Dclippy::no_mangle_with_rust_abi \ -Wclippy::dbg_macro KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS) diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs index 397a3dd57a9b13..280676993074c1 100644 --- a/rust/kernel/allocator.rs +++ b/rust/kernel/allocator.rs @@ -31,16 +31,19 @@ static ALLOCATOR: KernelAllocator = KernelAllocator; // let's generate them ourselves instead. // // Note that `#[no_mangle]` implies exported too, nowadays. +#[allow(clippy::no_mangle_with_rust_abi)] #[no_mangle] fn __rust_alloc(size: usize, _align: usize) -> *mut u8 { unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 } } +#[allow(clippy::no_mangle_with_rust_abi)] #[no_mangle] fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) { unsafe { bindings::kfree(ptr as *const core::ffi::c_void) }; } +#[allow(clippy::no_mangle_with_rust_abi)] #[no_mangle] fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 { unsafe { @@ -52,6 +55,7 @@ fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize } } +#[allow(clippy::no_mangle_with_rust_abi)] #[no_mangle] fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 { unsafe {