Skip to content

Commit

Permalink
Fix for #585 - false positive NO_BRACES_IN_CONDITIONALS_AND_LOOPS whe…
Browse files Browse the repository at this point in the history
…n there is a comment in when entry (#591)

### What's done:
* Fixed logic
* Added test
  • Loading branch information
petertrr authored Nov 30, 2020
1 parent 11d2c4c commit 047ad05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import com.pinterest.ktlint.core.ast.ElementType.WHILE
import com.pinterest.ktlint.core.ast.ElementType.WHILE_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.nextSibling
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.prevSibling
import org.cqfn.diktat.ruleset.utils.findChildrenMatching
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.constants.Warnings.NO_BRACES_IN_CONDITIONALS_AND_LOOPS
import org.cqfn.diktat.ruleset.utils.isSingleLineIfElse
import org.cqfn.diktat.ruleset.utils.prettyPrint
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -130,7 +133,10 @@ class BracesInConditionalsAndLoopsRule(private val configRules: List<RulesConfig
.asSequence()
.filter { it.expression != null && it.expression!!.node.elementType == BLOCK }
.map { it.expression as KtBlockExpression }
.filter { it.statements.size == 1 }
.filter { block ->
block.statements.size == 1 &&
block.findChildrenMatching { it.isPartOfComment() }.isEmpty()
}
.forEach {
NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, "WHEN", it.node.startOffset, it.node) {
it.astReplace(it.firstStatement!!.node.psi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ class BracesRuleWarnTest : LintTestBase(::BracesInConditionalsAndLoopsRule) {
| OPTION_3 -> {
| baz()
| }
| OPTION_4 -> {
| // description
| bzz()
| }
| }
|}
""".trimMargin(),
Expand Down

0 comments on commit 047ad05

Please sign in to comment.