Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config for variable expression in when #5681

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -718,6 +718,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"
}
}