Skip to main content
Version: 1.23.6

Libraries Rule Set

Rules in this rule set report issues related to libraries API exposure.

Note: The libraries rule set is not included in the detekt-cli or Gradle plugin.

To enable this rule set, add detektPlugins "io.gitlab.arturbosch.detekt:detekt-rules-libraries:$version" to your Gradle dependencies or reference the detekt-rules-libraries-jar with the --plugins option in the command line interface.

ForbiddenPublicDataClass

Data classes are bad for binary compatibility in public APIs. Avoid using them.

This rule is aimed at library maintainers. If you are developing a final application you can ignore this issue.

More info: Public API challenges in Kotlin

Active by default: Yes - Since v1.16.0

Debt: 20min

Configuration options:

  • ignorePackages (default: ['*.internal', '*.internal.*'])

    ignores classes in the specified packages.

Noncompliant Code:

data class C(val a: String) // violation: public data class

Compliant Code:

internal data class C(val a: String)

LibraryCodeMustSpecifyReturnType

Functions/properties exposed as public APIs of a library should have an explicit return type. Inferred return type can easily be changed by mistake which may lead to breaking changes.

See also: Kotlin 1.4 Explicit API

Active by default: Yes - Since v1.2.0

Requires Type Resolution

Debt: 5min

Configuration options:

  • allowOmitUnit (default: false)

    if functions with Unit return type should be allowed without return type declaration

Noncompliant Code:

// code from a library
val strs = listOf("foo, bar")
fun bar() = 5
class Parser {
fun parse() = ...
}

Compliant Code:

// code from a library
val strs: List<String> = listOf("foo, bar")
fun bar(): Int = 5

class Parser {
fun parse(): ParsingResult = ...
}

LibraryEntitiesShouldNotBePublic

Library typealias and classes should be internal or private.

Active by default: Yes - Since v1.16.0

Debt: 5min

Noncompliant Code:

// code from a library
class A

Compliant Code:

// code from a library
internal class A