Why do you believe it’s a false-positive/false-negative? The rule doesn’t account for how Equals and GetHashCode are overridden on record types.
Are you using
SonarQube for IDE - which IDE/version? - VS2022 / SonarQube for Visual Studio 2022 v8.9.0.11507
How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
When I create an empty record SonarQube reports issue S2094 saying there is no reason to have an empty class, and that it should be an interface instead.
However, the benefit of using an empty record is that I can then check if two instances of that type are equal and it will then check if the two instances have the same type and if yes do they have the same property values. This is helpful when using things like Dictionaries.
So in a sense, the record is not an empty class, but comes with an invisible overriden implementation of Equals and GetHashCode.
For instance, if you run the following code you will see it prints true when the variables are declared using record types, but false when the variables are declared using the interface.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(new EmptyRecord() == new EmptyRecord()); // True
AbstractRecord recordFromAbstractA = new Record("Test");
AbstractRecord recordFromAbstractB = new Record("Test");
Console.WriteLine(recordFromAbstractA == recordFromAbstractB); // True
IInterface recordFromInterfaceA = new Record("Test");
IInterface recordFromInterfaceB = new Record("Test");
Console.WriteLine(recordFromInterfaceA == recordFromInterfaceB); // False
}
private interface IInterface;
private abstract record AbstractRecord;
private record Record(string Name) : AbstractRecord, IInterface;
private record EmptyRecord;
}
Therefore I suggest disabling the rule for record types.
welcome to our community!
I don’t fully get the reasoning behind this, what would be the use case exactly?
I doesn’t seem to me a FP, I will discuss this internally and come back if we reconsider.
In the meantime, I suggest you disabling the rule or accepting these issues.
Incidentally, the fact that you can be sure only reference record types will ever inherit from the base abstract record is another point in its favour over an empty interface.