Skip to content

Commit

Permalink
S1694: Promote C# to SonarWay and remove protected constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Jun 4, 2024
1 parent 8c2eb7d commit 849b06e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
4 changes: 3 additions & 1 deletion rules/S1694/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{

"defaultQualityProfiles": [
"Sonar way"
]
}
33 changes: 1 addition & 32 deletions rules/S1694/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

The purpose of an abstract class is to provide some heritable behaviors while also defining methods which must be implemented by sub-classes.


A ``++class++`` with no abstract methods that was made ``++abstract++`` purely to prevent instantiation should be converted to a concrete ``++class++`` (i.e. remove the ``++abstract++`` keyword) with a ``++protected++`` constructor.


A ``++class++`` with only ``++abstract++`` methods and no inheritable behavior should be converted to an ``++interface++``.

=== Noncompliant code example
Expand All @@ -17,18 +13,6 @@ public abstract class Animal // Noncompliant; should be an interface
abstract void Move();
abstract void Feed();
}
public abstract class Color // Noncompliant; should be concrete with a protected constructor
{
private int red = 0;
private int green = 0;
private int blue = 0;
public int GetRed()
{
return red;
}
}
----

=== Compliant solution
Expand All @@ -41,21 +25,6 @@ public interface Animal
void Feed();
}
public class Color
{
private int red = 0;
private int green = 0;
private int blue = 0;
protected Color()
{}
public int GetRed()
{
return red;
}
}
public abstract class Lamp
{
private bool switchLamp = false;
Expand All @@ -81,7 +50,7 @@ ifdef::env-github,rspecator-view[]

=== Message

Convert this "abstract" (class|record) to (an interface|a concrete type with a private constructor).
Convert this "abstract" (class|record) to an interface.


'''
Expand Down

0 comments on commit 849b06e

Please sign in to comment.