From 15613afa205060b3979528d42646a8ce1d60baa7 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 15 Oct 2019 10:00:42 +0900 Subject: [PATCH] Control flow with empty body: do not report 'if' with comments #KT-34325 Fixed --- .../ControlFlowWithEmptyBodyInspection.kt | 5 +++-- .../if/blockHasCommentWithElse.kt | 12 ++++++++++++ .../inspections/LocalInspectionTestGenerated.java | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasCommentWithElse.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt index ed9bc3279b5..9285c702535 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ControlFlowWithEmptyBodyInspection.kt @@ -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) } diff --git a/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasCommentWithElse.kt b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasCommentWithElse.kt new file mode 100644 index 00000000000..bb45cf2eac4 --- /dev/null +++ b/idea/testData/inspectionsLocal/controlFlowWithEmptyBody/if/blockHasCommentWithElse.kt @@ -0,0 +1,12 @@ +// PROBLEM: none + +fun test(i: Int) { + if (i == 1) { + // comment + } else { + return + } + foo() +} + +fun foo() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 3a27dcb3a3a..5b95ff7d9bd 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -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");