-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new lint no_mangle_with_rust_abi #10369
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,65 @@ | ||
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, | ||
pedantic, | ||
"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()); | ||
let mut applicability = Applicability::MachineApplicable; | ||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut applicability); | ||
for attr in attrs { | ||
if let Some(ident) = attr.ident() | ||
&& ident.name == rustc_span::sym::no_mangle | ||
&& fn_sig.header.abi == Abi::Rust | ||
&& !snippet.contains("extern") { | ||
|
||
let suggestion = snippet.split_once("fn") | ||
.map_or(String::new(), |(first, second)| format!(r#"{first}extern "C" fn{second}"#)); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ pub unsafe fn mutates_static() -> usize { | |
} | ||
|
||
#[no_mangle] | ||
pub fn unmangled(i: bool) -> bool { | ||
pub extern "C" fn unmangled(i: bool) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just applying this lint. Helps us avoid adding noise to |
||
!i | ||
} | ||
|
||
|
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,48 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::no_mangle_with_rust_abi)] | ||
|
||
#[no_mangle] | ||
extern "C" fn rust_abi_fn_one(arg_one: u32, arg_two: usize) {} | ||
|
||
#[no_mangle] | ||
pub extern "C" 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 extern "C" 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 extern "C" fn rust_abi_fn_four(arg_one: u32, arg_two: usize) {} | ||
|
||
#[no_mangle] | ||
extern "C" fn rust_abi_multiline_function_really_long_name_to_overflow_args_to_multiple_lines( | ||
arg_one: u32, | ||
arg_two: usize, | ||
) -> u32 { | ||
0 | ||
} | ||
|
||
// Must not run on functions that explicitly opt in to Rust ABI with `extern "Rust"` | ||
#[no_mangle] | ||
#[rustfmt::skip] | ||
extern "Rust" fn rust_abi_fn_explicit_opt_in(arg_one: u32, arg_two: usize) {} | ||
|
||
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) {} | ||
|
||
extern "C" { | ||
fn c_abi_in_block(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,48 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused)] | ||
nindalf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![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 | ||
} | ||
|
||
// Must not run on functions that explicitly opt in to Rust ABI with `extern "Rust"` | ||
#[no_mangle] | ||
#[rustfmt::skip] | ||
extern "Rust" fn rust_abi_fn_explicit_opt_in(arg_one: u32, arg_two: usize) {} | ||
|
||
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) {} | ||
|
||
extern "C" { | ||
fn c_abi_in_block(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:7: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:10: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:15: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:20: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:23: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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to move this to
src/attrs.rs
or let it remain in it's own file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to leave in its own file.