From 02c8e4cede678c252a7af2c0883d5fb3f85fc61b Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Thu, 28 Mar 2019 13:43:02 +0300 Subject: [PATCH] New J2K: Do not remove semicolon after if expression when then branch is empty in RedundantSemicolonInspection --- .../RedundantSemicolonInspection.kt | 6 +++++ .../inspections/redundantSemicolon/Test.kt | 1 + .../inspectionData/expected.xml | 22 +++++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt index d3fd3316ef6..deeba1cb7e2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.* +import org.jetbrains.kotlin.utils.addToStdlib.safeAs class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { @@ -79,6 +80,11 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns return false } + semicolon.prevLeaf()?.parent?.safeAs()?.also { ifExpression -> + if (ifExpression.then == null) + return false + } + if (nextLeaf?.nextLeaf { it !is PsiWhiteSpace && it !is PsiComment && it.getStrictParentOfType() == null && it.getStrictParentOfType() == null diff --git a/idea/testData/inspections/redundantSemicolon/Test.kt b/idea/testData/inspections/redundantSemicolon/Test.kt index 6ba26d1f1ce..84c99e99807 100644 --- a/idea/testData/inspections/redundantSemicolon/Test.kt +++ b/idea/testData/inspections/redundantSemicolon/Test.kt @@ -34,6 +34,7 @@ fun bar() { fun baz(args: Array) { for (arg in args); while (args.size > 0); + if (args.size > 0); // But here redundant! do while (args.size > 0); } diff --git a/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml b/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml index f5ac0d931d8..40cd67facae 100644 --- a/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml +++ b/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml @@ -72,16 +72,7 @@ Test.kt - 38 - light_idea_test_case - - Redundant semicolon - Redundant semicolon - - - - Test.kt - 43 + 39 light_idea_test_case Redundant semicolon @@ -108,7 +99,16 @@ Test.kt - 49 + 46 + light_idea_test_case + + Redundant semicolon + Redundant semicolon + + + + Test.kt + 50 light_idea_test_case Redundant semicolon