A function 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 functions that are never referenced from inside a translation unit, and cannot be referenced from the outside.
Code examples
Noncompliant code example
class Foo: Serializable {
private fun unusedMethod() {...}
private fun writeObject(s: ObjectOutputStream) {...} // Compliant, relates to the serialization mechanism
private fun readObject(s: ObjectOutputStream) {...} // Compliant, relates to the serialization mechanism
}
Compliant solution
class Foo: Serializable {
private fun writeObject(s: ObjectOutputStream) {...} // Compliant, relates to the serialization mechanism
private fun readObject(s: ObjectOutputStream) {...} // Compliant, relates to the serialization mechanism
}
Exceptions
This rule doesn’t raise issues for:
- annotated methods
- methods with parameters that are annotated with
@javax.enterprise.event.Observes
The rule does not take reflection into account, which means that issues will be raised on private
methods that are only accessed using
the reflection API.