KT.STRING_LITERAL_DUPLICATION

Duplicate String literal

This rule detects and reports duplicated String literals. Repeatedly typing out the same String literal across the codebase makes it harder to change and maintain. Instead, prefer extracting the String literal into a property or constant.

Noncompliant Code

Copy
class Foo {
  val s1 = "lorem"
  fun bar(s: String = "lorem") {
    s1.equals("lorem")
  }
}

Compliant Code

Copy
class Foo {
  val lorem = "lorem"
  val s1 = lorem
  fun bar(s: String = lorem) {
    s1.equals(lorem)
  }
}

Options

  • excludes (default: ['**/test/**', '**/androidtest/**', '**/commontest/**', '**/jvmtest/**', '**/jstest/**', '**/iostest/**'])

    path filters

  • threshold (default: 3)

    amount of duplications to trigger rule

  • ignoreAnnotation (default: True)

    if values in Annotations should be ignored

  • excludeStringsWithLessThan5Characters (default: True)

    if short strings should be excluded

  • ignoreStringsRegex (default: $^)

    RegEx of Strings that should be ignored

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html