sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

New Rule Idea: UnsafeAccessorAttribute method should match the signature of its target

Open antonioaversa opened this issue 1 year ago • 0 comments

Related to C# 12 Zero-overhead member access.

Using UnsafeAccessorAttribute on a extern method with the parameters not matching the target results in a System.MissingMethodException Method not found.

using System.Runtime.CompilerServices;

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
extern static UserData CallPrivateConstructor(int i); // Noncompliant: there is no constructor with this signature

extern static ref string GetName(UserData obj);

_ = CallPrivateConstructor(42);

class UserData
{
    private UserData(string name) { }
}

Related new rule ideas on C# 12 Zero-overhead member access:

  • https://github.com/SonarSource/sonar-dotnet/issues/8151
  • https://github.com/SonarSource/sonar-dotnet/issues/8155

antonioaversa avatar Oct 06 '23 11:10 antonioaversa