mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
OptionalWhenBraces: fix false negative when the single statement has comments inside (#4722)
This commit is contained in:
committed by
GitHub
parent
69772a7ea8
commit
683e696375
@@ -7,10 +7,12 @@ 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.hasCommentInside
|
||||
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtLambdaExpression
|
||||
import org.jetbrains.kotlin.psi.KtWhenExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||
|
||||
/**
|
||||
* This rule reports unnecessary braces in when expressions. These optional braces should be removed.
|
||||
@@ -50,10 +52,11 @@ class OptionalWhenBraces(config: Config = Config.empty) : Rule(config) {
|
||||
super.visitWhenExpression(expression)
|
||||
}
|
||||
|
||||
private fun KtBlockExpression.hasUnnecessaryBraces(): Boolean {
|
||||
val singleStatement = statements.singleOrNull()?.takeIf {
|
||||
!(it is KtLambdaExpression && it.functionLiteral.arrow == null)
|
||||
}
|
||||
return singleStatement != null && !hasCommentInside() && lBrace != null && rBrace != null
|
||||
}
|
||||
private fun KtBlockExpression.hasUnnecessaryBraces(): Boolean =
|
||||
lBrace != null && rBrace != null &&
|
||||
statements.singleOrNull()?.takeIf { !it.isLambdaExpressionWithoutArrow() } != null &&
|
||||
allChildren.none { it is PsiComment }
|
||||
|
||||
private fun KtExpression.isLambdaExpressionWithoutArrow(): Boolean =
|
||||
this is KtLambdaExpression && this.functionLiteral.arrow == null
|
||||
}
|
||||
|
||||
@@ -72,6 +72,27 @@ class OptionalWhenBracesSpec {
|
||||
.hasSourceLocations(SourceLocation(7, 17), SourceLocation(10, 17))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `reports unnecessary braces when the single statement has comments inside`() {
|
||||
val code = """
|
||||
fun test(i: Int) {
|
||||
when {
|
||||
else -> {
|
||||
when (i) {
|
||||
// foo
|
||||
1 -> println(1)
|
||||
// bar
|
||||
else -> println(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
assertThat(subject.compileAndLint(code))
|
||||
.hasSize(1)
|
||||
.hasSourceLocations(SourceLocation(3, 9))
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class `the statement is a lambda expression` {
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user