2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
include::../description.adoc[]
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
include::../howtofix.adoc[]
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2023-10-18 10:16:10 +02:00
|
|
|
Alternatively, adding the `static` keyword as class modifier will also prevent it from being instantiated.
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
=== Code examples
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
public class StringUtils // Noncompliant: implicit public constructor
|
|
|
|
{
|
|
|
|
public static string Concatenate(string s1, string s2)
|
|
|
|
{
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
[source,csharp,diff-id=2,diff-type=noncompliant]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
2023-06-15 15:32:44 +02:00
|
|
|
public class StringUtils // Noncompliant: explicit public constructor
|
2020-06-30 12:47:33 +02:00
|
|
|
{
|
2023-06-15 15:32:44 +02:00
|
|
|
public StringUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:47:33 +02:00
|
|
|
public static string Concatenate(string s1, string s2)
|
|
|
|
{
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
==== Compliant solution
|
2020-06-30 12:47:33 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
2023-10-18 10:16:10 +02:00
|
|
|
public static class StringUtils // Compliant: the class is static
|
2020-06-30 12:47:33 +02:00
|
|
|
{
|
|
|
|
public static string Concatenate(string s1, string s2)
|
|
|
|
{
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
or
|
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
[source,csharp,diff-id=2,diff-type=compliant]
|
2020-06-30 12:47:33 +02:00
|
|
|
----
|
2023-10-18 10:16:10 +02:00
|
|
|
public class StringUtils // Compliant: the constructor is not public
|
2020-06-30 12:47:33 +02:00
|
|
|
{
|
2023-10-18 10:16:10 +02:00
|
|
|
private StringUtils()
|
2020-06-30 12:47:33 +02:00
|
|
|
{
|
|
|
|
}
|
2023-06-15 15:32:44 +02:00
|
|
|
|
2020-06-30 12:47:33 +02:00
|
|
|
public static string Concatenate(string s1, string s2)
|
|
|
|
{
|
|
|
|
return s1 + s2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2023-06-15 15:32:44 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Hide this public constructor by making it ["protected"|"private"].
|
|
|
|
|
|
|
|
Add a ["protected"|"private"] constructor or the "static" keyword to the class declaration.
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2023-05-25 14:18:12 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|