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 S3253 FP: Don't raise for primary constructor in type declarations without parameters when they inherit from types with parameters #8436

Closed
thomasfroehle opened this issue Dec 4, 2023 · 3 comments · Fixed by #9400
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@thomasfroehle
Copy link

thomasfroehle commented Dec 4, 2023

Description

S3253 is reported when a record with a parameterless constructor inherits another record that has parameters and passes them inline (instead of having an explicit base()-call).

Repro steps

public abstract record Base(string Value);

public record Foo() : Base("SomeValue");

Expected behavior

No S3253 is raised because the Foo()-constructor is not redundant.

Actual behavior

S3253 is raised.

Known workarounds

public abstract record Base(string Value);

public record Foo : Base
{
    public Foo() : base("SomeValue")
    {

    }
}

Related information

SonarAnalyzer.CSharp: 9.14.0.81108

@thomasfroehle thomasfroehle changed the title Fix S3253 FP: Fix S3253 FP: Record without parameters inherits from record with parameters raises S3253 Dec 4, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource removed their assignment Dec 8, 2023
@gregory-paidis-sonarsource gregory-paidis-sonarsource added Area: C# C# rules related issues. Area: C#9 Type: False Positive Rule IS triggered when it shouldn't be. and removed Area: C# C# rules related issues. labels Dec 8, 2023
@gregory-paidis-sonarsource
Copy link
Contributor

Hey there!
I confirm that this is indeed a false positive.
I made a reproducer to track this, feel free to take a look.

Just so you know, this rule is not part of the default, SonarWay profile, so updates to it will come a bit slower than SonarWay rules.
Until this is fixed, feel free to disable the rule from your profile if it is becoming too noisy for you. :)

@thomasfroehle
Copy link
Author

Thanks for the feedback. The repro fits perfect to the issue.

@DominikAmon
Copy link

Please note, that is also affects regular classes as well (not only records)

Example:

public class ClaimsIdentityExpectedException() : CodeException(Properties.Resources.TtWkisEx0001ClaimsIdentityExpectedException,Properties.Resources.TtWkisEx0001ClaimsIdentityExpectedCode)
{
}

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: C# C# rules related issues. and removed Area: C#9 labels Feb 19, 2024
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.27 milestone Jun 4, 2024
@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title Fix S3253 FP: Record without parameters inherits from record with parameters raises S3253 Fix S3253 FP: Don't raise for primary constructor in records without parameters when they inherit from record with parameters Jun 4, 2024
@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title Fix S3253 FP: Don't raise for primary constructor in records without parameters when they inherit from record with parameters Fix S3253 FP: Don't raise for primary constructor in type declarations without parameters when they inherit from types with parameters Jun 6, 2024
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
7 participants