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

NO_BRACES_IN_CONDITIONALS_AND_LOOPS fail within variable initialization block #1739

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -13,6 +13,7 @@
import org.jetbrains.kotlin.KtNodeTypes.BLOCK
import org.jetbrains.kotlin.KtNodeTypes.CALL_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.IF
import org.jetbrains.kotlin.KtNodeTypes.LAMBDA_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.SAFE_ACCESS_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.WHEN
Expand Down Expand Up @@ -66,12 +67,7 @@
val thenNode = ifPsi.then?.node
val elseKeyword = ifPsi.elseKeyword
val elseNode = ifPsi.`else`?.node
val indent = node
.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
val indent = node.findIndentBeforeNode()

if (node.isSingleLineIfElse()) {
return
Expand Down Expand Up @@ -132,11 +128,8 @@
if (loopBodyNode == null || loopBodyNode.elementType != BLOCK) {
NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, node.elementType.toString(), node.startOffset, node) {
// fixme proper way to calculate indent? or get step size (instead of hardcoded 4)
val indent = node.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
val indent = node.findIndentBeforeNode()

loopBody?.run {
replaceWithBlock(indent)
}
Expand All @@ -152,6 +145,20 @@
}
}

private fun ASTNode.findIndentBeforeNode(): Int {
val indentNode = if (treeParent?.treeParent?.treeParent?.elementType == LAMBDA_EXPRESSION) {
treeParent.prevSibling { it.elementType == WHITE_SPACE }
DrAlexD marked this conversation as resolved.
Show resolved Hide resolved
} else {
prevSibling { it.elementType == WHITE_SPACE }
DrAlexD marked this conversation as resolved.
Show resolved Hide resolved
}

return indentNode!!
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
.text
.lines()
.last()
.count { it == ' ' }
}

@Suppress("UnsafeCallOnNullableType")
private fun checkWhenBranches(node: ASTNode) {
(node.psi as KtWhenExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class BracesRuleFixTest : FixTestBase("test/paragraph3/braces", ::BracesInCondit
fixAndCompare("LoopsBracesExpected.kt", "LoopsBracesTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to if-else statements inside scope functions`() {
fixAndCompare("IfElseBracesInsideScopeFunctionsExpected.kt", "IfElseBracesInsideScopeFunctionsTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to loops inside scope functions`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) {
foo()
} else {
bar()
}
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else {
bar()
}
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) {
foo()
} else {
bar()
}
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) {
foo()
} else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) {
foo()
} else {
bar()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) foo()
else bar()
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else bar()
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) foo()
else bar()
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) foo()
else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) foo()
else bar()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@ package test.paragraph3.braces
fun foo1() {
str.apply {
for (i in 1..100) {
println(i)
}
println(i)
}
}
}

fun foo2() {
str.let {
for (i in 1..100) {
println(i)
}
str.let { while (x > 0) {
println(i)
}
}
}

fun foo3() {
str.run {
for (i in 1..100) {
println(i)
}
do {
println(i)
}
while (x > 0)
}
}

fun foo4() {
str.with {
for (i in 1..100) {
println(i)
}
str.with { do {
println(i)
}
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) {
println(i)
}
while (x > 0)
{
println(i)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ fun foo1() {
}

fun foo2() {
str.let {
for (i in 1..100) println(i)
str.let { while (x > 0) println(i)
}
}

fun foo3() {
str.run {
for (i in 1..100) println(i)
do println(i)
while (x > 0)
}
}

fun foo4() {
str.with {
for (i in 1..100) println(i)
str.with { do println(i)
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) println(i)
for (i in 1..100) {
while (x > 0)
println(i)
}
}
}
Loading