-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint no_mangle_with_rust_abi
- Loading branch information
Showing
9 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_target::spec::abi::Abi; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for Rust ABI functions with the `#[no_mangle]` attribute. | ||
/// | ||
/// ### Why is this bad? | ||
/// The Rust ABI is not stable, but in many simple cases matches | ||
/// enough with the C ABI that it is possible to forget to add | ||
/// `extern "C"` to a function called from C. Changes to the | ||
/// Rust ABI can break this at any point. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// #[no_mangle] | ||
/// fn example(arg_one: u32, arg_two: usize) {} | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```rust | ||
/// #[no_mangle] | ||
/// extern "C" fn example(arg_one: u32, arg_two: usize) {} | ||
/// ``` | ||
#[clippy::version = "1.69.0"] | ||
pub NO_MANGLE_WITH_RUST_ABI, | ||
suspicious, | ||
"convert Rust ABI functions to C ABI" | ||
} | ||
declare_lint_pass!(NoMangleWithRustAbi => [NO_MANGLE_WITH_RUST_ABI]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for NoMangleWithRustAbi { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { | ||
if let ItemKind::Fn(fn_sig, _, _) = &item.kind { | ||
let attrs = cx.tcx.hir().attrs(item.hir_id()); | ||
for attr in attrs { | ||
if let Some(ident) = attr.ident() | ||
&& ident.name.as_str() == "no_mangle" | ||
&& fn_sig.header.abi == Abi::Rust { | ||
|
||
let mut applicability = Applicability::MachineApplicable; | ||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut applicability); | ||
let suggestion = snippet.split_once("fn") | ||
.map(|(first, second)| format!(r#"{first}extern "C" fn{second}"#)) | ||
.unwrap_or("".to_string()); | ||
span_lint_and_sugg( | ||
cx, | ||
NO_MANGLE_WITH_RUST_ABI, | ||
fn_sig.span, | ||
"attribute #[no_mangle] set on a Rust ABI function", | ||
"try", | ||
suggestion, | ||
applicability | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::no_mangle_with_rust_abi)] | ||
|
||
#[no_mangle] | ||
fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {} | ||
|
||
#[no_mangle] | ||
pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {} | ||
|
||
|
||
/// # Safety | ||
/// This function shouldn't be called unless the horsemen are ready | ||
#[no_mangle] | ||
pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {} | ||
|
||
/// # Safety | ||
/// This function shouldn't be called unless the horsemen are ready | ||
#[no_mangle] | ||
unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {} | ||
|
||
#[no_mangle] | ||
fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines( | ||
arg_one: u32, | ||
arg_two: usize, | ||
) -> u32 { | ||
0 | ||
} | ||
|
||
fn rust_abi_fn_again(arg_one: u32, arg_two: usize) {} | ||
|
||
#[no_mangle] | ||
extern "C" fn c_abi_fn(arg_one: u32, arg_two: usize) {} | ||
|
||
extern "C" fn c_abi_fn_again(arg_one: u32, arg_two: usize) {} | ||
|
||
fn main() { | ||
// test code goes here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error: attribute #[no_mangle] set on a Rust ABI function | ||
--> $DIR/no_mangle_with_rust_abi.rs:5:1 | ||
| | ||
LL | fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `extern "C" fn rust_abi_fn_one(arg_one: u32, arg_two: usize)` | ||
| | ||
= note: `-D clippy::no-mangle-with-rust-abi` implied by `-D warnings` | ||
|
||
error: attribute #[no_mangle] set on a Rust ABI function | ||
--> $DIR/no_mangle_with_rust_abi.rs:8:1 | ||
| | ||
LL | pub fn rust_abi_fn_two(arg_one: u32, arg_two: usize) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `pub extern "C" fn rust_abi_fn_two(arg_one: u32, arg_two: usize)` | ||
|
||
error: attribute #[no_mangle] set on a Rust ABI function | ||
--> $DIR/no_mangle_with_rust_abi.rs:14:1 | ||
| | ||
LL | pub unsafe fn rust_abi_fn_three(arg_one: u32, arg_two: usize) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `pub unsafe extern "C" fn rust_abi_fn_three(arg_one: u32, arg_two: usize)` | ||
|
||
error: attribute #[no_mangle] set on a Rust ABI function | ||
--> $DIR/no_mangle_with_rust_abi.rs:19:1 | ||
| | ||
LL | unsafe fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unsafe extern "C" fn rust_abi_fn_four(arg_one: u32, arg_two: usize)` | ||
|
||
error: attribute #[no_mangle] set on a Rust ABI function | ||
--> $DIR/no_mangle_with_rust_abi.rs:22:1 | ||
| | ||
LL | / fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines( | ||
LL | | arg_one: u32, | ||
LL | | arg_two: usize, | ||
LL | | ) -> u32 { | ||
| |________^ | ||
| | ||
help: try | ||
| | ||
LL + extern "C" fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines( | ||
LL + arg_one: u32, | ||
LL + arg_two: usize, | ||
LL ~ ) -> u32 { | ||
| | ||
|
||
error: aborting due to 5 previous errors | ||
|