rspec/rules/S1144/csharp/rule.adoc

59 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-10-20 15:29:34 +02:00
:operationName: type or member
:visibility: private/internal
2023-10-20 15:29:34 +02:00
include::../why.adoc[]
2023-06-14 14:04:05 +02:00
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
2023-06-14 14:04:05 +02:00
private void UnusedPrivateMethod(){...} // Noncompliant, this private method is unused and can be removed.
2020-06-30 12:47:33 +02:00
2023-06-14 14:04:05 +02:00
private class UnusedClass {...} // Noncompliant, unused private class that can be removed.
2020-06-30 12:47:33 +02:00
}
----
2023-06-14 14:04:05 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-06-14 14:04:05 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
public Foo()
{
UsedPrivateMethod();
}
2023-10-20 15:29:34 +02:00
private void UsedPrivateMethod()
2020-06-30 12:47:33 +02:00
{
var c = new UsedClass();
2023-10-20 15:29:34 +02:00
}
2020-06-30 12:47:33 +02:00
private class UsedClass {...}
}
----
2023-10-20 15:29:34 +02:00
=== Exceptions
This rule doesn't raise issues on:
* empty constructors
* members with attributes
* the `Main` method of the application
* methods with event handler signature `void Foo(object, EventArgs)` that are declared in partial class
* empty serialization constructor on type with https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute[System.SerializableAttribute] attribute.
* field and property members of types marked with https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute[System.SerializableAttribute] attribute
2023-10-20 15:29:34 +02:00
* internal members in assemblies that have a https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute[System.Runtime.CompilerServices.InternalsVisibleToAttribute] attribute.
2023-06-14 14:04:05 +02:00
== Resources
2023-06-14 14:04:05 +02:00
=== Documentation
2023-06-14 14:04:05 +02:00
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers[Access Modifiers (C# Programming Guide)]
2023-06-14 14:04:05 +02:00
include::rspecator.adoc[]