org.sonar.plugins.csharp.S1144.html Maven / Gradle / Ivy
This rule raises an issue when a private/internal type or member is never referenced in the code.
Why is this an issue?
A type or member that is never called is dead code, and 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.
This rule detects type or members that are never referenced from inside a translation unit, and cannot be referenced from the outside.
Exceptions
This rule doesn’t raise issues on:
- empty constructors
- members with attributes
- the
Main
method of the application
-
void
methods with two parameters when the second parameter type derives from EventArgs
- empty serialization constructor on type with System.SerializableAttribute attribute.
- field and property members of types marked with System.SerializableAttribute attribute
- internal members in assemblies that have a System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute.
- types and members decorated with the System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute attribute (available in .NET 5.0+) or a custom attribute named
DynamicallyAccessedMembersAttribute
.
Code examples
Noncompliant code example
public class Foo
{
private void UnusedPrivateMethod(){...} // Noncompliant, this private method is unused and can be removed.
private class UnusedClass {...} // Noncompliant, unused private class that can be removed.
}
Compliant solution
public class Foo
{
public Foo()
{
UsedPrivateMethod();
}
private void UsedPrivateMethod()
{
var c = new UsedClass();
}
private class UsedClass {...}
}
Resources
Documentation
© 2015 - 2024 Weber Informatics LLC | Privacy Policy