Skip to content

Commit c1ab18d

Browse files
committed
no_mangle_with_rust_abi: properly position the suggested ABI
1 parent 6631a2c commit c1ab18d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

clippy_lints/src/no_mangle_with_rust_abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ impl<'tcx> LateLintPass<'tcx> for NoMangleWithRustAbi {
4040
if let ItemKind::Fn(fn_sig, _, _) = &item.kind {
4141
let attrs = cx.tcx.hir().attrs(item.hir_id());
4242
let mut app = Applicability::MaybeIncorrect;
43-
let fn_snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app);
43+
let fn_snippet = snippet_with_applicability(cx, fn_sig.span.with_hi(item.ident.span.lo()), "..", &mut app);
4444
for attr in attrs {
4545
if let Some(ident) = attr.ident()
4646
&& ident.name == rustc_span::sym::no_mangle
4747
&& fn_sig.header.abi == Abi::Rust
48-
&& let Some((fn_attrs, _)) = fn_snippet.split_once("fn")
48+
&& let Some((fn_attrs, _)) = fn_snippet.rsplit_once("fn")
4949
&& !fn_attrs.contains("extern")
5050
{
5151
let sugg_span = fn_sig

tests/ui/no_mangle_with_rust_abi.rs

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ extern "C" {
4848
fn c_abi_in_block(arg_one: u32, arg_two: usize);
4949
}
5050

51+
mod r#fn {
52+
#[unsafe(no_mangle)]
53+
pub(in super::r#fn) fn with_some_fn_around() {}
54+
}
55+
5156
fn main() {
5257
// test code goes here
5358
}

tests/ui/no_mangle_with_rust_abi.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,20 @@ help: or explicitly set the default
7979
LL | extern "Rust" fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines(
8080
| +++++++++++++
8181

82-
error: aborting due to 5 previous errors
82+
error: `#[unsafe(no_mangle)]` set on a function with the default (`Rust`) ABI
83+
--> tests/ui/no_mangle_with_rust_abi.rs:53:5
84+
|
85+
LL | pub(in super::r#fn) fn with_some_fn_around() {}
86+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
|
88+
help: set an ABI
89+
|
90+
LL | pub(in super::r#fn) extern "C" fn with_some_fn_around() {}
91+
| ++++++++++
92+
help: or explicitly set the default
93+
|
94+
LL | pub(in super::r#fn) extern "Rust" fn with_some_fn_around() {}
95+
| +++++++++++++
96+
97+
error: aborting due to 6 previous errors
8398

0 commit comments

Comments
 (0)