SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
798 stars 229 forks source link

Fix S2325 FP: "Make 'MethodName' a static method." should not apply to overriding instance methods #9651

Closed richardissimo closed 3 months ago

richardissimo commented 3 months ago

Description

S2325 Make 'MethodName' a static method. If I have an abstract class with a virtual method, and the derived class has an implementation which could be made static, this should not trigger this rule, because it is overriding the virtual.

Repro steps

    public abstract class DemoProblem
    {
        protected virtual bool IsAllowed()
        {
            return true;
        }
    }

    public class DerivedClass : DemoProblem
    {
        public bool IsAllowed() // Triggers S2325
        {
            return false;
        }
    }

Expected behavior

Should not trigger S2325

Actual behavior

triggers S2325

Known workarounds

Suppress rule

Related information

richardissimo commented 3 months ago

Hmm. My made up example isn't exactly what I had for real, and I can see in that example, the DerivedClass implementation should override. I'll see if I can find a better example, closer to my problem.

richardissimo commented 3 months ago

No, I'm withdrawing this. This is more like what the actual code looks like, so fair enough, that can be made static.

    public abstract class Rule
    {
        protected string SimplifyWhitespaceExpression(string expression)
        {
            // Implementation isn't relevant, but it can be made static
        }