Skip to content
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 1 commit into from
Feb 23, 2023
Merged

Conversation

nindalf
Copy link
Contributor

@nindalf nindalf commented Feb 18, 2023

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 and Abi == 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

@rustbot
Copy link
Collaborator

rustbot commented Feb 18, 2023

r? @llogiq

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 18, 2023
@nindalf nindalf changed the title Add new lint no_mangle_with_rust_abi [WIP] Add new lint no_mangle_with_rust_abi Feb 18, 2023
@nindalf nindalf force-pushed the no_mangle_lint branch 2 times, most recently from 2dd9898 to eaafb8e Compare February 18, 2023 17:05
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_target::spec::abi::Abi;

declare_clippy_lint! {
Copy link
Contributor Author

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?

Copy link
Contributor

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)),
Copy link
Contributor Author

@nindalf nindalf Feb 18, 2023

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.

Copy link
Contributor Author

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")

@nindalf nindalf force-pushed the no_mangle_lint branch 3 times, most recently from b845985 to 46c251b Compare February 18, 2023 20:06
@@ -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 {
Copy link
Contributor Author

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")
Copy link
Contributor Author

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?

@nindalf nindalf changed the title [WIP] Add new lint no_mangle_with_rust_abi Add new lint no_mangle_with_rust_abi Feb 18, 2023
@nindalf nindalf force-pushed the no_mangle_lint branch 2 times, most recently from 36ded2a to 07922c0 Compare February 18, 2023 20:49
@nindalf nindalf marked this pull request as ready for review February 18, 2023 20:59
@nindalf nindalf changed the title Add new lint no_mangle_with_rust_abi Add lint that converts Rust ABI functions with the #[no_mangle] attribute to C ABI Feb 18, 2023
@nindalf nindalf changed the title Add lint that converts Rust ABI functions with the #[no_mangle] attribute to C ABI Add new lint no_mangle_with_rust_abi Feb 18, 2023
@nindalf nindalf force-pushed the no_mangle_lint branch 2 times, most recently from 38e3a15 to 2b431ca Compare February 20, 2023 13:08
if let Some(ident) = attr.ident()
&& ident.name == rustc_span::sym::no_mangle
&& fn_sig.header.abi == Abi::Rust
&& !snippet.contains("extern"){
Copy link
Contributor Author

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.

@nindalf nindalf force-pushed the no_mangle_lint branch 3 times, most recently from dc1a37b to d7a8d17 Compare February 20, 2023 13:24
@bors
Copy link
Contributor

bors commented Feb 23, 2023

☔ The latest upstream changes (presumably #10392) made this pull request unmergeable. Please resolve the merge conflicts.

@nindalf nindalf force-pushed the no_mangle_lint branch 2 times, most recently from 7bbff8d to ee64b43 Compare February 23, 2023 17:05
@llogiq
Copy link
Contributor

llogiq commented Feb 23, 2023

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+

@bors
Copy link
Contributor

bors commented Feb 23, 2023

📌 Commit 00c294a has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 23, 2023

⌛ Testing commit 00c294a with merge 659112c...

@bors
Copy link
Contributor

bors commented Feb 23, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing 659112c to master...

@bors bors merged commit 659112c into rust-lang:master Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants