Control flow with empty body: do not report 'if' with comments

#KT-34325 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-10-15 10:00:42 +09:00
committed by Dmitry Gridin
parent 615d3aafef
commit 15613afa20
3 changed files with 20 additions and 2 deletions
@@ -18,10 +18,11 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
class ControlFlowWithEmptyBodyInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = object : KtVisitorVoid() {
override fun visitIfExpression(expression: KtIfExpression) {
if (expression.then.isEmptyBodyOrNull()) {
val then = expression.then
val elseKeyword = expression.elseKeyword
if (then.isEmptyBodyOrNull() && (elseKeyword == null || then?.hasComments() != true)) {
holder.registerProblem(expression, expression.ifKeyword)
}
val elseKeyword = expression.elseKeyword
if (elseKeyword != null && expression.`else`.isEmptyBodyOrNull()) {
holder.registerProblem(expression, elseKeyword)
}
@@ -0,0 +1,12 @@
// PROBLEM: none
fun test(i: Int) {
<caret>if (i == 1) {
// comment
} else {
return
}
foo()
}
fun foo() {}
@@ -2730,6 +2730,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasComment.kt");
}
@TestMetadata("blockHasCommentWithElse.kt")
public void testBlockHasCommentWithElse() throws Exception {
runTest("idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasCommentWithElse.kt");
}
@TestMetadata("blockHasElse.kt")
public void testBlockHasElse() throws Exception {
runTest("idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasElse.kt");