From 059a797e10fe9c4f566ec5ee2041f546936440d2 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 8 Feb 2018 11:22:45 +0300 Subject: [PATCH] Don't report redundant semicolon for a call before lambda expression So #KT-22719 Fixed --- .../inspections/RedundantSemicolonInspection.kt | 17 ++++++++++------- .../beforeCommentAndLambda.kt | 8 ++++++++ .../LocalInspectionTestGenerated.java | 6 ++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 idea/testData/inspectionsLocal/redundantSemicolon/beforeCommentAndLambda.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt index 218616147de..f736eb495d0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSemicolonInspection.kt @@ -34,17 +34,19 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns super.visitElement(element) if (element.node.elementType == KtTokens.SEMICOLON && isRedundant(element)) { - holder.registerProblem(element, - "Redundant semicolon", - ProblemHighlightType.LIKE_UNUSED_SYMBOL, - Fix) + holder.registerProblem( + element, + "Redundant semicolon", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + Fix + ) } } } } private fun isRedundant(semicolon: PsiElement): Boolean { - val nextLeaf = semicolon.nextLeaf { it !is PsiWhiteSpace && it !is PsiComment || it.isLineBreak() } + val nextLeaf = semicolon.nextLeaf { it !is PsiWhiteSpace && it !is PsiComment || it.isLineBreak() } val isAtEndOfLine = nextLeaf == null || nextLeaf.isLineBreak() if (!isAtEndOfLine) { //when there is no imports parser generates empty import list with no spaces @@ -59,7 +61,8 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns (semicolon.parent.parent as? KtClass)?.let { if (it.isEnum() && it.getChildrenOfType().isEmpty()) { if (semicolon.prevLeaf { it !is PsiWhiteSpace && it !is PsiComment && !it.isLineBreak() }?.node?.elementType == KtTokens.LBRACE - && it.declarations.isNotEmpty()) { + && it.declarations.isNotEmpty() + ) { //first semicolon in enum with no entries, but with some declarations return false } @@ -71,7 +74,7 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns return false } - if (nextLeaf?.nextLeaf { it !is PsiComment }?.node?.elementType == KtTokens.LBRACE) { + if (nextLeaf?.nextLeaf { it !is PsiWhiteSpace && 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/inspectionsLocal/redundantSemicolon/beforeCommentAndLambda.kt b/idea/testData/inspectionsLocal/redundantSemicolon/beforeCommentAndLambda.kt new file mode 100644 index 00000000000..804a2a1a94b --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantSemicolon/beforeCommentAndLambda.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +fun test() { + foo(); + // comment + { i: Int -> }.doIt() +} +fun foo() {} +fun ((Int) -> Unit).doIt() {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 010fbedfc5e..b71d7eb13a5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -2823,6 +2823,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantSemicolon"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("beforeCommentAndLambda.kt") + public void testBeforeCommentAndLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSemicolon/beforeCommentAndLambda.kt"); + doTest(fileName); + } + @TestMetadata("companionBeforeFun.kt") public void testCompanionBeforeFun() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantSemicolon/companionBeforeFun.kt");