Skip to content

Commit

Permalink
rust: enable no_mangle_with_rust_abi Clippy lint
Browse files Browse the repository at this point in the history
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 is no option to prevent that, not even
`force_explicit_abi` [3]). Therefore, use an `allow` 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 <ojeda@kernel.org>
  • Loading branch information
ojeda committed Jun 5, 2023
1 parent e458d45 commit 95fef1a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions rust/kernel/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 95fef1a

Please sign in to comment.