Skip to content

Commit

Permalink
Don't report when entries with multiple conditions and trailing comma
Browse files Browse the repository at this point in the history
  • Loading branch information
AfzalivE committed Sep 13, 2024
1 parent 59dc3c9 commit a6c563a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/doist/detekt/ConsistentWhenEntries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ class ConsistentWhenEntries(config: Config) : Rule(config) {
}
}

private fun KtWhenEntry.isMultiline() = text.count { it == '\n' } > 1
private fun KtWhenEntry.isMultiline() = (expression?.text?.count { it == '\n' } ?: 0) > 1
}
34 changes: 34 additions & 0 deletions src/test/kotlin/com/doist/detekt/ConsistentWhenEntriesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,38 @@ internal class ConsistentWhenEntriesTest(private val env: KotlinCoreEnvironment)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report single line when entries with multiple conditions and trailing comma`() {
val code = """
val a = listOf<Int>()
val b = when(a) {
is ArrayList,
is MutableList,
-> true
else -> false
}
"""
val rule = ConsistentWhenEntries(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `report inconsistent when entries with multiple conditions and trailing comma`() {
val code = """
val a = listOf<Int>()
val b = when(a) {
is ArrayList,
is MutableList,
-> {
true
}
else -> false
}
"""
val rule = ConsistentWhenEntries(Config.empty)
val findings = rule.compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}
}

0 comments on commit a6c563a

Please sign in to comment.