Skip to content

#[allow(unsafe_op_in_unsafe_fn)] sometimes ignored when the impl body is generated by a macro #19823

Closed
@todesking

Description

@todesking

rust-analyzer version: rust-analyzer 1.87.0 (17067e9ac6d 2025-05-09) and rust-analyzer 1.89.0-nightly (4d051fb3 2025-05-18)

rustc version: rustc 1.87.0 (17067e9ac 2025-05-09) and rustc 1.89.0-nightly (4d051fb30 2025-05-18)

editor or extension: Neovim 0.11.0 + Mason + nvim-lspconfig

code snippet to reproduce:

pub trait FooTrait {
    unsafe fn method1();
    unsafe fn method2();
}

unsafe fn some_unsafe_fn() {}

macro_rules! impl_foo {
    () => {
        unsafe fn method1() {
            some_unsafe_fn();
        }
        unsafe fn method2() {
            some_unsafe_fn();
        }
    };
}

pub struct S1;
// Works fine: no diagnostics.
#[allow(unsafe_op_in_unsafe_fn)]
impl FooTrait for S1 {
    unsafe fn method1() {
        some_unsafe_fn();
    }

    unsafe fn method2() {
        some_unsafe_fn();
    }
}

pub struct S2;
// Expectation: identical to S1 (no diagnostics)
// Actual: rust-analyzer shows an `unsafe_op_in_unsafe_fn` warning on the macro expansion.
#[allow(unsafe_op_in_unsafe_fn)]
impl FooTrait for S2 {
    // [WARN] call to unsafe function is unsafe and requires an unsafe function or block [unsafe_op_in_unsafe_fn]
    impl_foo!();
}

Run cargo check on the same code: no warnings are emitted.

Activity

ChayimFriedman2

ChayimFriedman2 commented on May 19, 2025

@ChayimFriedman2
Contributor

Okay that is weird - if we comment the non-macro impl the macro impl no longer warns...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsdiagnostics / error reportingC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @todesking@Veykril@ChayimFriedman2

      Issue actions

        `#[allow(unsafe_op_in_unsafe_fn)]` sometimes ignored when the impl body is generated by a macro · Issue #19823 · rust-lang/rust-analyzer