Skip to content

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

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

Closed
todesking opened this issue May 19, 2025 · 1 comment · Fixed by #19824
Labels
A-diagnostics diagnostics / error reporting C-bug Category: bug

Comments

@todesking
Copy link

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.

@todesking todesking added the C-bug Category: bug label May 19, 2025
@Veykril Veykril added the A-diagnostics diagnostics / error reporting label May 19, 2025
@ChayimFriedman2
Copy link
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
Labels
A-diagnostics diagnostics / error reporting C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants