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 UseLet style rule #6091

Merged
merged 6 commits into from
May 17, 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
2 changes: 2 additions & 0 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ style:
active: true
UseIsNullOrEmpty:
active: true
UseLet:
active: true
UseOrEmpty:
active: true
UseRequire:
Expand Down
2 changes: 2 additions & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ style:
ignoreWhenContainingVariableDeclaration: false
UseIsNullOrEmpty:
active: true
UseLet:
active: false
UseOrEmpty:
active: true
UseRequire:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class StyleGuideProvider : DefaultRuleSetProvider {
AlsoCouldBeApply(config),
UseSumOfInsteadOfFlatMapSize(config),
DoubleNegativeLambda(config),
UseLet(config),
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
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.rules.isNonNullCheck
import io.gitlab.arturbosch.detekt.rules.isNullCheck
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.KtPsiUtil

/**
* `if` expressions that either check for not-null and return `null` in the false case or check for `null` and returns
* `null` in the truthy case are better represented as `?.let {}` blocks.
*
* <noncompliant>
* if (x != null) { x.transform() } else null
* if (x == null) null else y
* </noncompliant>
*
* <compliant>
* x?.let { it.transform() }
* x?.let { y }
* </compliant>
*/
class UseLet(
config: Config = Config.empty
) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Style,
"Use `?.let {}` instead of if/else with a null block when checking for nullable values",
Debt.FIVE_MINS
)

private fun isExpressionNull(branch: KtExpression?): Boolean {
val statement = when (branch) {
is KtBlockExpression -> if (branch.statements.size == 1) branch.statements.first() else null
is KtConstantExpression -> branch
else -> null
}

return statement?.let { KtPsiUtil.isNullConstant(it) } ?: false
}

override fun visitIfExpression(expression: KtIfExpression) {
super.visitIfExpression(expression)
val condition = expression.condition as? KtBinaryExpression ?: return

if (condition.isNullCheck() && isExpressionNull(expression.then)) {
report(CodeSmell(issue, Entity.from(expression), issue.description))
} else if (condition.isNonNullCheck() && isExpressionNull(expression.`else`)) {
report(CodeSmell(issue, Entity.from(expression), issue.description))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.gitlab.arturbosch.detekt.rules.style

import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory

class UseLetSpec {

val subject = UseLet()

@TestFactory
fun `it forbids all != null else null combinations`(): Iterable<DynamicTest> {
val conditions = listOf(
Triple("1 == null", false, true),
Triple("null == 1", false, true),
Triple("1 == 1", false, false),
Triple("null == null", false, true),
Triple("1 != null", true, false),
Triple("null != 1", true, false),
Triple("1 != 1", false, false),
Triple("null != null", true, false),
)

val exprs = listOf(
"1" to false,
"null" to true,
"{ 1 }" to false,
"{ null }" to true,
)

return conditions.flatMap { (condition, isNonNullCheck, isNullCheck) ->
exprs.flatMap { (left, leftIsNull) ->
exprs.map { (right, rightIsNull) ->
DynamicTest.dynamicTest("($condition) $left else $right") {
val expr = "fun test() = if ($condition) $left else $right"
val shouldFail = (isNonNullCheck && rightIsNull) || (isNullCheck && leftIsNull)
val findings = subject.compileAndLint(expr)
if (shouldFail) {
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasMessage(subject.issue.description)
} else {
assertThat(findings).isEmpty()
}
}
}
}
}
}

@Test
fun `do not capture blocks that have multiple expressions`() {
val findings = subject.compileAndLint(
"""
fun testCallToCreateTempFile(s: String?) {
val x = if (s == null) {
println("foo")
null
} else {
"x"
}
}
""".trimIndent()
)

assertThat(findings).isEmpty()
}

@Test
fun `it allows the following expressions (currently)`() {
val findings = subject.compileAndLint(
"""
fun testCallToCreateTempFile() {
val x: String? = "abc"
if (x == null) println(x) else null
if (x is String) println(x)
if (x != null) { println(x) }
}
""".trimIndent()
)

assertThat(findings).isEmpty()
}
}