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

Fix S2292 FN: Support primary constructor parameters

Open antonioaversa opened this issue 1 year ago • 0 comments

Extend https://sonarsource.github.io/rspec/#/rspec/S2292 to support also parameters in C# 12 primary constructors.

For example, the following property:

public class Car(string make)
{
    public string Make // Noncompliant
    {
        get { return make; }
        set { make = value; }
    }
}

can be transformed into an auto-implemented property which is assigned the primary constructor parameter at constructor time:

public class Car(string make)
{
    public string Make { get; set; } = make;
}

That produces IL representations that are slightly different (IL of first example vs IL of second example) but essentially the same, and basically differ only by the name of the unspeakable field generated (<make>P in the first example, <Make>k__BackingField in the second example).

antonioaversa avatar Sep 26 '23 14:09 antonioaversa