Don't report redundant semicolon for a call before lambda expression

So #KT-22719 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-02-08 11:22:45 +03:00
committed by Mikhail Glukhikh
parent f9555f3f64
commit 059a797e10
3 changed files with 24 additions and 7 deletions
@@ -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<KtEnumEntry>().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
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun test() {
foo()<caret>;
// comment
{ i: Int -> }.doIt()
}
fun foo() {}
fun ((Int) -> Unit).doIt() {}
@@ -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");