-
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
Conversation
r? @llogiq (rustbot has picked a reviewer for you, use r? to override) |
2dd9898
to
eaafb8e
Compare
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_target::spec::abi::Abi; | ||
|
||
declare_clippy_lint! { |
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.
span, | ||
&format!("attribute #[no_mangle] set on a Rust ABI function"), | ||
"try", | ||
format!(r#"extern "C" {}"#, snippet_with_applicability(cx, span, "..", &mut applicability)), |
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.
@llogiq I'm not sure about the best way to accomplish this. I have the entire span
available here - pub fn example()
, which needs to be modified to pub extern "C" fn example()
. Is there an elegant way to do this that doesn't involve string splitting?
I didn't find anything relevant in the Span
docs.
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.
Ended up using String::split_once("fn")
b845985
to
46c251b
Compare
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Just applying this lint. Helps us avoid adding noise to tests/ui/must_use_candidates.fixed
|
||
let mut applicability = Applicability::MachineApplicable; | ||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut applicability); | ||
let suggestion = snippet.split_once("fn") |
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.
Is using String::split_once
acceptable?
46c251b
to
d7bf2d4
Compare
36ded2a
to
07922c0
Compare
#[no_mangle]
attribute to C ABI
#[no_mangle]
attribute to C ABI38e3a15
to
2b431ca
Compare
if let Some(ident) = attr.ident() | ||
&& ident.name == rustc_span::sym::no_mangle | ||
&& fn_sig.header.abi == Abi::Rust | ||
&& !snippet.contains("extern"){ |
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.
Is there a better way to do this that doesn't involve string search? Can we search on the span directly? I wasn't able to find this information in the Function Header.
dc1a37b
to
d7a8d17
Compare
☔ The latest upstream changes (presumably #10392) made this pull request unmergeable. Please resolve the merge conflicts. |
7bbff8d
to
ee64b43
Compare
ee64b43
to
00c294a
Compare
Thank you for writing this lint! Sorry it took me so long to review, I had a lot of stuff on my desk after returning from London. @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes issue #10347
This PR adds a new lint
no_mangle_with_rust_abi
that suggests converting a function to the C ABI to if the function has the#[no_mangle]
attribute andAbi == Abi::Rust
. It will not run for any of the other variants defined in rustc_target::spec::abi::Abi, nor suggest any conversion other than conversion to the C ABI.Functions that explicitly opt into the Rust ABI with
extern "Rust"
are ignored by this lint.changelog: [
no_mangle_with_rust_abi
]: add lint that converts Rust ABI functions with the#[no_mangle]
attribute to C ABI