Skip to content

unsafe_op_in_unsafe_fn suggestion overlaps when used in a macro #123304

Closed
@ehuss

Description

@ehuss

The following example triggers the unsafe_op_in_unsafe_fn lint twice in the same position:

#![warn(unsafe_op_in_unsafe_fn)]

macro_rules! foo {
    ($x:ident) => {
        pub unsafe fn $x() {
            let _ = String::new().as_mut_vec();
        }
    };
}

foo!(a);
foo!(b);

This causes cargo fix to apply both suggestions which ends up with:

// ...snip
pub unsafe fn $x() { unsafe { unsafe {
    let _ = String::new().as_mut_vec();
}}}
// ...snip

which triggers the unused_unsafe lint.

Would it be possible to avoid duplicate suggestions when used in a macro?
cc @asquared31415

I suspect this may be encountered frequently with the 2024 edition migration (crater run is pending in #122960). Although it isn't too much of a problem to repair, a frequently used macro could have many unsafe blocks, which could be confusing.

rustc 1.79.0-nightly (8df7e723e 2024-03-30)
binary: rustc
commit-hash: 8df7e723ea729a7f917501cc2d91d640b7021373
commit-date: 2024-03-30
host: aarch64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2024Area: The 2024 editionC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions