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 @@ -132,11 +132,11 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
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 == ' ' }
val indent = node.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
loopBody?.run {
replaceWithBlock(indent)
}
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 loops inside scope functions`() {
fixAndCompare("LoopsBracesInsideScopeFunctionsExpected.kt", "LoopsBracesInsideScopeFunctionsTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
for (i in 1..100) {
println(i)
}
}
}

fun foo2() {
str.let {
for (i in 1..100) {
println(i)
}
}
}

fun foo3() {
str.run {
for (i in 1..100) {
println(i)
}
}
}

fun foo4() {
str.with {
for (i in 1..100) {
println(i)
}
}
}

fun foo5() {
str.also {
for (i in 1..100) {
println(i)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
for (i in 1..100) println(i)
}
}

fun foo2() {
str.let {
for (i in 1..100) println(i)
}
}

fun foo3() {
str.run {
for (i in 1..100) println(i)
}
}

fun foo4() {
str.with {
for (i in 1..100) println(i)
}
}

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