S2325 should take into consideration DynamicallyAccessedMembersAttribute

  • What language is this for? C#
  • Which rule? S2325
  • Why do you believe it’s a false-positive/false-negative?

When a method or property in a class does not access instance members, SonarQube raises a S2325 warning indicating that the member should be made static. However, that fails to take into consideration scenarios where the member is dynamically accessed, meaning that its modifiers are part of its API, and changing that can be a breaking change.

C# already has the DynamicallyAccessedMembersAttribute whose purpose is precisely to indicate that a member can be dynamically accessed (its main use case currently is trimming); However, SonarQube is currently oblivious to that in relation to applying S2325.

I think usage of DynamicallyAccessedMembersAttribute should be added to the list of exceptions of S2325.

  • Are you using

    • SonarQube Cloud? No
    • SonarQube Server / Community Build - which version? No
    • SonarQube for IDE - which IDE/version? SonarQube for Visual Studio version 8.29.0.14599
      • in connected mode with SonarQube Server / Community Build or SonarQube Cloud? No
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots):

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors |
                          DynamicallyAccessedMemberTypes.PublicMethods |
                          DynamicallyAccessedMemberTypes.PublicProperties)]
public class Foo
{
    public int Bar => 1;
}
1 Like

I agree. That would be a huge improvement.