Skip to content

[Analyzer]: Flagging unexpected UnsafeAccessorAttribute usage #95361

Open
@AaronRobinsonMSFT

Description

@AaronRobinsonMSFT

The UnsafeAccessorAttribute feature has some non-obvious issues in some scenarios. The most recent example is captured in #91740.

Example:

using System.Runtime.CompilerServices;

class Program
{
    static void Main()
    {
        // Crashes at runtime with System.MissingMethodException:
        // Method not found: 'System.Object.<Main>g__MemberwiseClone|0_0'.
        _ = MemberwiseClone(new object());

        [UnsafeAccessor(UnsafeAccessorKind.Method)] // Analyzer should warn when applied to local function without defining "Name" property.
        static extern object MemberwiseClone(object obj);
    }
}

Additional cases for analyzer to consider: #91740 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions