rspec/rules/S1144/csharp/rule.adoc

66 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
``++private++`` or ``++internal++`` types or ``++private++`` members that are never executed or referenced are dead code: unnecessary, inoperative code that should be removed. Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.
2020-06-30 12:47:33 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
private void UnusedPrivateMethod() {...} // Noncompliant
private class UnusedClass {...} // Noncompliant
}
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
public Foo()
{
UsedPrivateMethod();
}
private void UsedPrivateMethod()
{
var c = new UsedClass();
}
private class UsedClass {...}
}
----
=== Exceptions
2020-06-30 12:47:33 +02:00
This rule doesn't raise issues on:
2020-06-30 12:47:33 +02:00
* Empty constructors
* Attributed members
2021-01-27 13:42:22 +01:00
* ``++Main++`` method
* Methods with event handler signature ``++void Foo(object, EventArgs)++`` that are declared in partial class
* Empty serialization constructor on type with ``++System.SerializableAttribute++`` attribute.
* Internals in assemblies that have a ``++System.Runtime.CompilerServices.InternalsVisibleToAttribute++`` attribute.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Remove this unused [private|internal] member.
* Remove this unused private property [getter|setter].
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]