mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-04 08:31:30 +00:00
Highlighting of redundant semicolons
#KT-5010 Fixed
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant semicolon (';') token which is not required in Kotlin and may be removed.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1461,6 +1461,14 @@
|
||||
level="WARNING"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RedundantSemicolonInspection"
|
||||
displayName="Redundant semicolon"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
cleanupTool="true"
|
||||
level="WARNING"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiComment
|
||||
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.psiUtil.nextLeaf
|
||||
|
||||
class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return object : PsiElementVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
super.visitElement(element)
|
||||
|
||||
if (element.node.elementType == KtTokens.SEMICOLON && isRedundant(element)) {
|
||||
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 isAtEndOfLine = nextLeaf == null || nextLeaf.isLineBreak()
|
||||
if (!isAtEndOfLine) return false
|
||||
|
||||
if (semicolon.parent is KtEnumEntry) 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
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun PsiElement?.isLineBreak() = this is PsiWhiteSpace && textContains('\n')
|
||||
|
||||
private object Fix : LocalQuickFix {
|
||||
override fun getName() = "Remove redundant semicolon"
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
descriptor.psiElement.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
32
idea/testData/inspections/redundantSemicolon/Test.kt
vendored
Normal file
32
idea/testData/inspections/redundantSemicolon/Test.kt
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
package p; // redundant
|
||||
|
||||
import java.util.ArrayList; // redundant
|
||||
|
||||
class A {
|
||||
fun foo() {
|
||||
print(1); print(2); // redundant
|
||||
}; // redundant
|
||||
|
||||
fun bar() {}
|
||||
}; // redundant
|
||||
|
||||
enum class E {
|
||||
A, B; // let's not consider it to be redundant
|
||||
}
|
||||
|
||||
fun foo(p: Int) {
|
||||
if (p > 0) foo(); // not redundant!
|
||||
|
||||
{ p: Int ->
|
||||
print(p)
|
||||
}.doIt()
|
||||
}
|
||||
|
||||
fun ((Int) -> Unit).doIt() {
|
||||
this.invoke(1); // redundant
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
a(); // redundant
|
||||
b()
|
||||
}
|
||||
64
idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml
vendored
Normal file
64
idea/testData/inspections/redundantSemicolon/inspectionData/expected.xml
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>1</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>3</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>7</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>8</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>11</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>26</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>Test.kt</file>
|
||||
<line>30</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="Test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||
<description>Redundant semicolon</description>
|
||||
</problem>
|
||||
</problems>
|
||||
1
idea/testData/inspections/redundantSemicolon/inspectionData/inspections.test
vendored
Normal file
1
idea/testData/inspections/redundantSemicolon/inspectionData/inspections.test
vendored
Normal file
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RedundantSemicolonInspection
|
||||
1
idea/testData/quickfix/redundantSemicolon/.inspection
vendored
Normal file
1
idea/testData/quickfix/redundantSemicolon/.inspection
vendored
Normal file
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RedundantSemicolonInspection
|
||||
5
idea/testData/quickfix/redundantSemicolon/afterImport.kt
vendored
Normal file
5
idea/testData/quickfix/redundantSemicolon/afterImport.kt
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// "Remove redundant semicolon" "true"
|
||||
import kotlin.*;<caret>
|
||||
|
||||
fun foo() {
|
||||
}
|
||||
5
idea/testData/quickfix/redundantSemicolon/afterImport.kt.after
vendored
Normal file
5
idea/testData/quickfix/redundantSemicolon/afterImport.kt.after
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// "Remove redundant semicolon" "true"
|
||||
import kotlin.*<caret>
|
||||
|
||||
fun foo() {
|
||||
}
|
||||
8
idea/testData/quickfix/redundantSemicolon/afterStatement.kt
vendored
Normal file
8
idea/testData/quickfix/redundantSemicolon/afterStatement.kt
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// "Remove redundant semicolon" "true"
|
||||
fun foo() {
|
||||
a();<caret>
|
||||
b()
|
||||
}
|
||||
|
||||
fun a(){}
|
||||
fun b(){}
|
||||
8
idea/testData/quickfix/redundantSemicolon/afterStatement.kt.after
vendored
Normal file
8
idea/testData/quickfix/redundantSemicolon/afterStatement.kt.after
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// "Remove redundant semicolon" "true"
|
||||
fun foo() {
|
||||
a()<caret>
|
||||
b()
|
||||
}
|
||||
|
||||
fun a(){}
|
||||
fun b(){}
|
||||
@@ -172,6 +172,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("redundantSemicolon/inspectionData/inspections.test")
|
||||
public void testRedundantSemicolon_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/redundantSemicolon/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("redundantVisibilityModifier/inspectionData/inspections.test")
|
||||
public void testRedundantVisibilityModifier_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/redundantVisibilityModifier/inspectionData/inspections.test");
|
||||
|
||||
@@ -6344,6 +6344,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/redundantSemicolon")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RedundantSemicolon extends AbstractQuickFixTest {
|
||||
@TestMetadata("afterImport.kt")
|
||||
public void testAfterImport() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantSemicolon/afterImport.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("afterStatement.kt")
|
||||
public void testAfterStatement() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantSemicolon/afterStatement.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInRedundantSemicolon() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/redundantSemicolon"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/redundantVisibilityModifier")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user