Skip to content

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

@a10r

Description

@a10r

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

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions