Mark semicolons after nullable types redundant, despite of '!' after (KT-38240)

This commit is contained in:
Yan Zhulanow
2020-05-29 19:29:04 +09:00
parent 4cf83d9526
commit 741ebeb7b8
4 changed files with 31 additions and 8 deletions
@@ -86,20 +86,30 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns
return false // case with statement starting with '{' and call on the previous line
}
return !isSemicolonAllowed(semicolon)
}
private fun isSemicolonAllowed(semicolon: PsiElement): Boolean {
if (isRequiredForCompanion(semicolon)) {
return false
return true
}
val prevSibling = semicolon.getPrevSiblingIgnoringWhitespaceAndComments()
val nextSibling = semicolon.getNextSiblingIgnoringWhitespaceAndComments()
if (prevSibling.safeAs<KtNameReferenceExpression>()?.text in softModifierKeywords &&
nextSibling is KtDeclaration
) return false
if (nextSibling.safeAs<KtPrefixExpression>()?.operationToken == KtTokens.EXCL &&
semicolon.prevLeaf()?.getStrictParentOfType<KtTypeReference>() != null
) return false
return true
if (prevSibling.safeAs<KtNameReferenceExpression>()?.text in softModifierKeywords && nextSibling is KtDeclaration) {
// enum; class Foo
return false
}
if (nextSibling is KtPrefixExpression && nextSibling.operationToken == KtTokens.EXCL) {
val typeElement = semicolon.prevLeaf()?.getStrictParentOfType<KtTypeReference>()?.typeElement
if (typeElement != null) {
return typeElement !is KtNullableType // trailing '?' fixes parsing
}
}
return false
}
private fun isRequiredForCompanion(semicolon: PsiElement): Boolean {
@@ -0,0 +1,4 @@
fun test(a: Any) {
val b = a as Boolean?;<caret>
!b
}
@@ -0,0 +1,4 @@
fun test(a: Any) {
val b = a as Boolean?
!b
}
@@ -8396,6 +8396,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/redundantSemicolon/beforeKDocAndLambda.kt");
}
@TestMetadata("betweenNullableTypeAndNotOperator.kt")
public void testBetweenNullableTypeAndNotOperator() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantSemicolon/betweenNullableTypeAndNotOperator.kt");
}
@TestMetadata("betweenSoftKeywordAndDeclaration.kt")
public void testBetweenSoftKeywordAndDeclaration() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantSemicolon/betweenSoftKeywordAndDeclaration.kt");