Detect redundant semicolon before empty import list (#1072)
#KT-16577 Fixed
This commit is contained in:
@@ -25,6 +25,8 @@ import com.intellij.psi.PsiElementVisitor
|
|||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.KtEnumEntry
|
import org.jetbrains.kotlin.psi.KtEnumEntry
|
||||||
|
import org.jetbrains.kotlin.psi.KtImportList
|
||||||
|
import org.jetbrains.kotlin.psi.KtPackageDirective
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
|
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
|
||||||
|
|
||||||
class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||||
@@ -46,7 +48,13 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns
|
|||||||
private fun isRedundant(semicolon: PsiElement): Boolean {
|
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()
|
val isAtEndOfLine = nextLeaf == null || nextLeaf.isLineBreak()
|
||||||
if (!isAtEndOfLine) return false
|
if (!isAtEndOfLine) {
|
||||||
|
//when there is no imports parser generates empty import list with no spaces
|
||||||
|
if (semicolon.parent is KtPackageDirective && (nextLeaf as? KtImportList)?.imports?.isEmpty() ?: false) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if (semicolon.parent is KtEnumEntry) return false
|
if (semicolon.parent is KtEnumEntry) return false
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package foo; // redundant
|
||||||
|
|
||||||
|
class A
|
||||||
@@ -61,4 +61,13 @@
|
|||||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||||
<description>Redundant semicolon</description>
|
<description>Redundant semicolon</description>
|
||||||
</problem>
|
</problem>
|
||||||
|
|
||||||
|
<problem>
|
||||||
|
<file>BeforeEmptyImports.kt</file>
|
||||||
|
<line>1</line>
|
||||||
|
<module>light_idea_test_case</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="BeforeEmptyImports.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant semicolon</problem_class>
|
||||||
|
<description>Redundant semicolon</description>
|
||||||
|
</problem>
|
||||||
</problems>
|
</problems>
|
||||||
|
|||||||
Reference in New Issue
Block a user