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

Fix: AVOID_NULL_CHECKS conflicts with NO_BRACES_IN_CONDITIONALS_AND_LOOPS #1191

Merged
merged 8 commits into from
Jan 27, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import org.cqfn.diktat.ruleset.utils.loopType

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.BLOCK
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.DO_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.ELSE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.IF
import com.pinterest.ktlint.core.ast.ElementType.IF_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.LBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.WHEN
import com.pinterest.ktlint.core.ast.ElementType.WHILE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
Expand All @@ -30,6 +33,7 @@ import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.astReplace
import org.jetbrains.kotlin.psi.psiUtil.children

/**
* Rule that checks that all conditionals and loops have braces.
Expand Down Expand Up @@ -90,6 +94,24 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}

if (elseKeyword != null && elseNode?.elementType != IF && elseNode?.elementType != BLOCK) {
// Looking for scope functions, for which we won't trigger
val callAndSafeAccessExpressionChildren = elseNode?.findChildrenMatching {
it.elementType == CALL_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION
}

val scopeFunctionChildren = callAndSafeAccessExpressionChildren?.flatMap {
it.children()
}?.filter {
it.elementType == REFERENCE_EXPRESSION
}

val isNodeHaveScopeFunctionChildren = scopeFunctionChildren?.any {
it.text in scopeFunctions
}
if (isNodeHaveScopeFunctionChildren == true) {
return
}

NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, "ELSE",
(elseNode?.treeParent?.prevSibling { it.elementType == ELSE_KEYWORD } ?: node).startOffset, node) {
elseNode?.run {
Expand Down Expand Up @@ -171,5 +193,6 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}
companion object {
private const val INDENT_STEP = 4
private val scopeFunctions = listOf("let", "run", "apply", "also")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,104 @@ class BracesRuleWarnTest : LintTestBase(::BracesInConditionalsAndLoopsRule) {
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements - exception for let`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else b?.let {
| baz()
| }
| ?: run {
| qux()
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements - exception for let 2`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else b?.let {
| baz()
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements - exception for run`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else b!!.run {
| baz()
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements - exception for apply`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else b.apply {
| baz()
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements, apply exists, but braces are needed`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else baz(b.apply { id = 5 })
|}
""".trimMargin(),
LintError(4, 7, ruleId, "${NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnText()} ELSE", true)
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should check braces in if statements, apply exists, but braces are needed 2`() {
lintMethod(
"""
|fun foo() {
| if (a) {
| bar()
| } else
| c.baz(b.apply {id = 5})
|}
""".trimMargin(),
LintError(4, 7, ruleId, "${NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnText()} ELSE", true)
)
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should correctly detect single line if`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@ fun foo4() {
if (x > 0) {
} else {
} ;
}
}

fun foo() {
if (a) {
bar()
} else b?.let {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add it to the smoke test too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

baz()
}
?: run {
qux()
}
}

fun foo() {
if (a) {
bar()
} else b.apply {
baz()
}
}

fun foo() {
if (a) {
bar()
} else {
baz(b.apply { id = 5 })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,29 @@ fun foo3() {
fun foo4() {
if (x > 0)
else ;
}
}

fun foo() {
if (a) {
bar()
} else b?.let {
baz()
}
?: run {
qux()
}
}

fun foo() {
if (a) {
bar()
} else b.apply {
baz()
}
}

fun foo() {
if (a) {
bar()
} else baz(b.apply { id = 5 })
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ fun foo() {
doSmth()
}
}

fun fooo() {
if (a) {
bar()
} else b?.let {
baz()
}
?: run {
qux()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ fun foo() {
}
}

fun fooo() {
if (a) {
bar()
} else b?.let {
baz()
}
?: run {
qux()
}
}