Mark semicolons after nullable types redundant, despite of '!' after (KT-38240)
This commit is contained in:
@@ -86,20 +86,30 @@ class RedundantSemicolonInspection : AbstractKotlinInspection(), CleanupLocalIns
|
|||||||
return false // case with statement starting with '{' and call on the previous line
|
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)) {
|
if (isRequiredForCompanion(semicolon)) {
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
val prevSibling = semicolon.getPrevSiblingIgnoringWhitespaceAndComments()
|
val prevSibling = semicolon.getPrevSiblingIgnoringWhitespaceAndComments()
|
||||||
val nextSibling = semicolon.getNextSiblingIgnoringWhitespaceAndComments()
|
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 {
|
private fun isRequiredForCompanion(semicolon: PsiElement): Boolean {
|
||||||
|
|||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
fun test(a: Any) {
|
||||||
|
val b = a as Boolean?;<caret>
|
||||||
|
!b
|
||||||
|
}
|
||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
fun test(a: Any) {
|
||||||
|
val b = a as Boolean?
|
||||||
|
!b
|
||||||
|
}
|
||||||
+5
@@ -8396,6 +8396,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/redundantSemicolon/beforeKDocAndLambda.kt");
|
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")
|
@TestMetadata("betweenSoftKeywordAndDeclaration.kt")
|
||||||
public void testBetweenSoftKeywordAndDeclaration() throws Exception {
|
public void testBetweenSoftKeywordAndDeclaration() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantSemicolon/betweenSoftKeywordAndDeclaration.kt");
|
runTest("idea/testData/inspectionsLocal/redundantSemicolon/betweenSoftKeywordAndDeclaration.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user