Skip to content

Commit

Permalink
Add config for variable expression in when (#5681)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicola Corti <corti.nico@gmail.com>
Fixes #5568
  • Loading branch information
atulgpt committed Jan 25, 2023
1 parent 2e6b576 commit ecb97fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ style:
active: false
UseIfInsteadOfWhen:
active: false
ignoreWhenContainingVariableDeclaration: false
UseIsNullOrEmpty:
active: true
UseOrEmpty:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtWhenExpression

/**
Expand Down Expand Up @@ -34,9 +37,15 @@ class UseIfInsteadOfWhen(config: Config = Config.empty) : Rule(config) {
Debt.FIVE_MINS
)

@Configuration("ignores when statements with a variable declaration used in the subject")
private val ignoreWhenContainingVariableDeclaration: Boolean by config(false)

@Suppress("ReturnCount")
override fun visitWhenExpression(expression: KtWhenExpression) {
super.visitWhenExpression(expression)

if (ignoreWhenContainingVariableDeclaration && expression.subjectExpression is KtProperty) return

if (expression.entries.size == 2 &&
expression.elseExpression != null &&
expression.entries.none { it.conditions.size > 1 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -64,4 +65,48 @@ class UseIfInsteadOfWhenSpec {
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}

// TC inspired from https://github.com/JetBrains/kotlin/pull/2921/files
@Test
fun `does not report 'when' variable declaration is present in subject expression when flag is true`() {
val code = """
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type) {
when (val i = t.ordinal) {
0 -> 1
else -> 42
}
}
""".trimIndent()
val subject = UseIfInsteadOfWhen(TestConfig(IGNORE_WHEN_CONTAINING_VARIABLE_DECLARATION to true))
assertThat(subject.compileAndLint(code)).isEmpty()
}

// TC inspired from https://github.com/JetBrains/kotlin/pull/2921/files
@Test
fun `does report 'when' variable declaration is present in subject expression when flag is false`() {
val code = """
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type) {
when (val i = t.ordinal) {
0 -> 1
else -> 42
}
}
""".trimIndent()
val subject = UseIfInsteadOfWhen(TestConfig(IGNORE_WHEN_CONTAINING_VARIABLE_DECLARATION to false))
assertThat(subject.compileAndLint(code)).hasSize(1)
}

companion object {
private const val IGNORE_WHEN_CONTAINING_VARIABLE_DECLARATION = "ignoreWhenContainingVariableDeclaration"
}
}

0 comments on commit ecb97fa

Please sign in to comment.