Skip to content

Commit 60526ad

Browse files
committed
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 c682e4c ("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: rust-lang/rust-clippy#10347 [1] Link: Rust-for-Linux#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 <[email protected]>
1 parent e458d45 commit 60526ad

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ export rust_common_flags := --edition=2021 \
466466
-Dclippy::let_unit_value -Dclippy::mut_mut \
467467
-Dclippy::needless_bitwise_bool \
468468
-Dclippy::needless_continue \
469+
-Dclippy::no_mangle_with_rust_abi \
469470
-Wclippy::dbg_macro
470471

471472
KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)

rust/kernel/allocator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ static ALLOCATOR: KernelAllocator = KernelAllocator;
3131
// let's generate them ourselves instead.
3232
//
3333
// Note that `#[no_mangle]` implies exported too, nowadays.
34+
#[allow(clippy::no_mangle_with_rust_abi)]
3435
#[no_mangle]
3536
fn __rust_alloc(size: usize, _align: usize) -> *mut u8 {
3637
unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KERNEL) as *mut u8 }
3738
}
3839

40+
#[allow(clippy::no_mangle_with_rust_abi)]
3941
#[no_mangle]
4042
fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
4143
unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
4244
}
4345

46+
#[allow(clippy::no_mangle_with_rust_abi)]
4447
#[no_mangle]
4548
fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize) -> *mut u8 {
4649
unsafe {
@@ -52,6 +55,7 @@ fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size: usize
5255
}
5356
}
5457

58+
#[allow(clippy::no_mangle_with_rust_abi)]
5559
#[no_mangle]
5660
fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 {
5761
unsafe {

0 commit comments

Comments
 (0)