From bf4e69e17f9334a1d03fc5bd051a312f7224bb26 Mon Sep 17 00:00:00 2001 From: Dmitry Neverov Date: Sun, 14 May 2017 20:44:26 +0300 Subject: [PATCH] Make semicolon in loop with empty body not redundant So #KT-12805 Fixed --- .../idea/inspections/RedundantSemicolonInspection.kt | 10 +++++++--- idea/testData/inspections/redundantSemicolon/Test.kt | 7 +++++++ .../redundantSemicolon/inspectionData/expected.xml | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt index 4b4d56cc96c..e9e9d767d54 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt @@ -24,10 +24,9 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.lexer.KtTokens -import org.jetbrains.kotlin.psi.KtEnumEntry -import org.jetbrains.kotlin.psi.KtImportList -import org.jetbrains.kotlin.psi.KtPackageDirective +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.nextLeaf +import org.jetbrains.kotlin.psi.psiUtil.prevLeaf class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { @@ -58,6 +57,11 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns if (semicolon.parent is KtEnumEntry) return false + (semicolon.prevLeaf()?.parent as? KtLoopExpression)?.let { + if (it !is KtDoWhileExpression && it.body == null) + return false + } + if (nextLeaf?.nextLeaf { it !is PsiComment }?.node?.elementType == KtTokens.LBRACE) { return false // case with statement starting with '{' and call on the previous line } diff --git a/idea/testData/inspections/redundantSemicolon/Test.kt b/idea/testData/inspections/redundantSemicolon/Test.kt index 3165ce05ecb..b09d9ec92b5 100644 --- a/idea/testData/inspections/redundantSemicolon/Test.kt +++ b/idea/testData/inspections/redundantSemicolon/Test.kt @@ -29,4 +29,11 @@ fun ((Int) -> Unit).doIt() { fun bar() { a(); // redundant b() +} + +fun baz(args: Array) { + for (arg in args); + while (args.size > 0); + // But here redundant! + do while (args.size > 0); } \ No newline at end of file diff --git a/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml b/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml index c2a30f94b7c..6508265824c 100644 --- a/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml +++ b/idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml @@ -70,4 +70,12 @@ Redundant semicolon Redundant semicolon + + Test.kt + 38 + light_idea_test_case + + Redundant semicolon + Redundant semicolon +