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 S2292 FN: Support primary constructor parameters #8078

Open
antonioaversa opened this issue Sep 26, 2023 · 0 comments
Open

Fix S2292 FN: Support primary constructor parameters #8078

antonioaversa opened this issue Sep 26, 2023 · 0 comments
Labels
Area: C#12 C#12 related issues Type: False Negative Rule is NOT triggered when it should be.

Comments

@antonioaversa
Copy link
Contributor

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).

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: False Negative Rule is NOT triggered when it should be. and removed Type: Improvement labels Jun 13, 2024
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Improve S2292: support primary constructor parameters Fix S2292 FN: Support primary constructor parameters Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C#12 C#12 related issues Type: False Negative Rule is NOT triggered when it should be.
Projects
None yet
Development

No branches or pull requests

2 participants