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 S2094 FP: Primary constructor calling base class constructor #9011

Closed
a10r opened this issue Apr 2, 2024 · 1 comment · Fixed by #9019
Closed

Fix S2094 FP: Primary constructor calling base class constructor #9011

a10r opened this issue Apr 2, 2024 · 1 comment · Fixed by #9019
Assignees
Labels
Area: C# C# rules related issues. Area: C#12 C#12 related issues Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@a10r
Copy link

a10r commented Apr 2, 2024

Description

A class with a primary ctor which calls a base class ctor but is otherwise empty produces a false positive S2094 (Remove this empty class, write its code or make it an "interface").

Repro steps

NUnit has the class TestCaseData which can be used to define parameters for test methods. Since the ctor of TestCaseData takes any objects, I often define a sub-class with a constructor to get some type safety and syntactic sugar that requires a known target type. With the new primary constructors in C# 12 this can be written in one line, for example:

public class CountTestCaseData(List<int> list, int expectedCount) : TestCaseData(list, expectedCount);

private static readonly List<CountTestCaseData> CountTestCases =
[
	new([1, 2, 3], 3)
];

Expected behavior

The class CountTestCaseData has some behavior so it should not be considered empty.

Actual behavior

The class CountTestCaseData produces S2094.

Known workarounds

Reverting back to the old but functionally identical C# syntax does not produce the warning:

public class CountTestCaseData : TestCaseData
{
	public CountTestCaseData(List<int> list, int expectedCount) : base(list, expectedCount) { }
};

Related information

  • C#/VB.NET Plugins version: 7.8.0.88494
  • Visual Studio version: 17.9.5
  • MSBuild / dotnet version: .NET 8
  • Operating System: Windows 11
@sebastien-marichal
Copy link
Contributor

Hello @a10r,

Thank you for reporting this issue!

I confirm this is an FP.

@sebastien-marichal sebastien-marichal added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. Area: C#12 C#12 related issues labels Apr 4, 2024
sebastien-marichal added a commit that referenced this issue Apr 4, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added this to the 9.24 milestone Apr 9, 2024
@github-actions github-actions bot added this to To do in Best Kanban Apr 9, 2024
Best Kanban automation moved this from To do to Validate Peach Apr 9, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource moved this from Validate Peach to Done in Best Kanban Apr 10, 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. Area: C#12 C#12 related issues Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
4 participants