Skip to content

S1694: Promote C# to SonarWay and remove protected constructor #3960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
]
}
35 changes: 2 additions & 33 deletions rules/S1694/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

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++``.
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