sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Rule idea: Replace protected parameterless empty constructor with abstract class

Open pavel-mikula-sonarsource opened this issue 1 year ago • 2 comments

Follow up of #8795

Suggest the exact opposite from what was originally suggested: If there's a parameterless protected constructor without any public/internal constructors, suggest making the class abstract to make the contract clear.

Ask to remove the constructor only if it's empty.

This would also trigger in a case like this, right?

class MyClass
{
    protected MyClass()
    {
    }
    
    public MyClass Factory()
    {
        return new MyClass();
        // do some setup
    }
}

I think there needs to be another condition.

Tim-Pohlmann avatar Feb 22 '24 10:02 Tim-Pohlmann

Maybe not instantiated inside as well... Does such a thing have a reason not to be sealed?