Skip to content
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

Fix S1144 FP: Diagnostic doesn't respect reflection with DynamicallyAccessedMembers attribute #9379

Closed
dotjpg3141 opened this issue Jun 3, 2024 · 2 comments · Fixed by #9406
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@dotjpg3141
Copy link

Description

Diagnostic S1144 doesn't respect reflection even though the generic parameter is annotated with the DynamicallyAccessedMembers attribute.

Related: #7774

Repro steps

This is a minified repro. In my actuall use case I called UseMiddleware.

using System.Diagnostics.CodeAnalysis;

namespace SonarCubeRepro
{
    internal static class Program
    {
        static void Main()
        {
            var instance = CreateInstance<Foo>();
            Console.WriteLine( instance.Number );
        }

        public static T CreateInstance<[DynamicallyAccessedMembers( DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors )] T>()
        {
            var instance = (T)Activator.CreateInstance( typeof( T ), 42 )!;
            return instance;
        }

        private sealed class Foo
        {
            public Foo( int number )
            {
                Number = number;
            }

            public int Number { get; set; }
        }
    }
}

Expected behavior

No warning.

Actual behavior

Warning S1144

Known workarounds

Supress the issue

Related information

SonarAnalyzer.CSharp Version=9.26.0.92422

  • C#/VB.NET Plugins version
  • Visual Studio version: 17.9.6
  • MSBuild / dotnet version: 8.0.204
  • SonarScanner for .NET version (if used)
  • Operating System: Windows 10
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 3, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added this to the 9.27 milestone Jun 3, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

Thank you for reporting the issue. Confirmed as False Positive.

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix S1144 FP: Diangostic doesn't respect reflection with DynamicallyAccessedMembers attribute Fix S1144 FP: Diagnostic doesn't respect reflection with DynamicallyAccessedMembers attribute Jun 4, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource modified the milestones: 9.27, 9.28 Jun 11, 2024
@zsolt-kolbay-sonarsource
Copy link
Contributor

@dotjpg3141 It turns out fixing the issue for generic method calls seriously degrades the analyzer performance, so we opted not to fix this scenario and instead document it as a False Positive.
The analyzer can still be prompted to ignore a type or a member by decorating it with the DynamicallyAccessedMembersAttribute: either by decorating it with the System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute (available in .NET 5.0+) or by your own custom attribute named DynamicallyAccessedMembersAttribute (in .NET Framework projects).
This change will be available in the next release (9.28).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants