Auto merge of #10420 - Jarcho:no_mangle_diag, r=dswij
Improve diagnostic of `no_mangle_with_rust_abi` fixes #10409 Pending rust-lang/rustfmt#5701 This rewords the message to focus on the error being an implicit ABI, rather than the `Rust` ABI. Also downgrades the suggestion to `MaybeIncorrect` and changes the suggestion span to better highlight the change. --- changelog: None <!-- changelog_checked -->
This commit is contained in:
commit
e426ba4e06
4 changed files with 71 additions and 81 deletions
|
|
@ -1,9 +1,10 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
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_span::{BytePos, Pos};
|
||||
use rustc_target::spec::abi::Abi;
|
||||
|
||||
declare_clippy_lint! {
|
||||
|
|
@ -38,25 +39,28 @@ 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);
|
||||
let mut app = Applicability::MaybeIncorrect;
|
||||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app);
|
||||
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 Some((fn_attrs, _)) = snippet.split_once("fn")
|
||||
&& !fn_attrs.contains("extern")
|
||||
{
|
||||
let sugg_span = fn_sig.span
|
||||
.with_lo(fn_sig.span.lo() + BytePos::from_usize(fn_attrs.len()))
|
||||
.shrink_to_lo();
|
||||
|
||||
let suggestion = snippet.split_once("fn")
|
||||
.map_or(String::new(), |(first, second)| format!(r#"{first}extern "C" fn{second}"#));
|
||||
|
||||
span_lint_and_sugg(
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
NO_MANGLE_WITH_RUST_ABI,
|
||||
fn_sig.span,
|
||||
"attribute #[no_mangle] set on a Rust ABI function",
|
||||
"try",
|
||||
suggestion,
|
||||
applicability
|
||||
"`#[no_mangle]` set on a function with the default (`Rust`) ABI",
|
||||
|diag| {
|
||||
diag.span_suggestion(sugg_span, "set an ABI", "extern \"C\" ", app)
|
||||
.span_suggestion(sugg_span, "or explicitly set the default", "extern \"Rust\" ", app);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue