Auto merge of #13631 - samueltardieu:push-uoowutzkvsrk, r=Centri3
no_mangle attribute requires unsafe in Rust 2024 Tests without unsafe must not run in edition 2024. Also, error messages have been modified to include the full attribute, so that a use of `#[unsafe(no_mangle)]` does not produce an error message containing `#[no_mangle]`. changelog: [`no_mangle_attribute`]: handle `#[unsafe(no_mangle)]` as well
This commit is contained in:
commit
a1a9aaef87
5 changed files with 164 additions and 23 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::source::snippet_with_applicability;
|
||||
use clippy_utils::source::{snippet, snippet_with_applicability};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Item, ItemKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
|
|
@ -18,13 +18,13 @@ declare_clippy_lint! {
|
|||
/// Rust ABI can break this at any point.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```no_run
|
||||
/// ```rust,ignore
|
||||
/// #[no_mangle]
|
||||
/// fn example(arg_one: u32, arg_two: usize) {}
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```no_run
|
||||
/// ```rust,ignore
|
||||
/// #[no_mangle]
|
||||
/// extern "C" fn example(arg_one: u32, arg_two: usize) {}
|
||||
/// ```
|
||||
|
|
@ -40,24 +40,25 @@ impl<'tcx> LateLintPass<'tcx> for NoMangleWithRustAbi {
|
|||
if let ItemKind::Fn(fn_sig, _, _) = &item.kind {
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let mut app = Applicability::MaybeIncorrect;
|
||||
let snippet = snippet_with_applicability(cx, fn_sig.span, "..", &mut app);
|
||||
let fn_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
|
||||
&& let Some((fn_attrs, _)) = snippet.split_once("fn")
|
||||
&& let Some((fn_attrs, _)) = fn_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 attr_snippet = snippet(cx, attr.span, "..");
|
||||
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
NO_MANGLE_WITH_RUST_ABI,
|
||||
fn_sig.span,
|
||||
"`#[no_mangle]` set on a function with the default (`Rust`) ABI",
|
||||
format!("`{attr_snippet}` 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