Unnecessary parentheses in function call with lambda: fix false positive #KT-26669 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-10-31 15:50:00 +09:00
committed by Mikhail Glukhikh
parent 2d0ecf8f50
commit 1df15612d6
5 changed files with 46 additions and 3 deletions
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.idea.conversion.copy.range
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
@@ -44,8 +44,9 @@ class RemoveEmptyParenthesesFromLambdaCallIntention : SelfTargetingRangeIntentio
val parent = element.parent as? KtCallExpression ?: return null
val singleLambdaArgument = parent.lambdaArguments.singleOrNull() ?: return null
if (element.getLineNumber(start = false) != singleLambdaArgument.getLineNumber(start = true)) return null
if (element.getPrevSiblingIgnoringWhitespaceAndComments() !is KtCallExpression) return element.range
return null
val prev = element.getPrevSiblingIgnoringWhitespaceAndComments()
if (prev is KtCallExpression || (prev as? KtQualifiedExpression)?.callExpression != null) return null
return element.range
}
override fun applyTo(element: KtValueArgumentList, editor: Editor?) {
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun Unit.returnFun3(fn: (Unit) -> Unit): ((Unit) -> Unit) -> Unit = {}
fun test() {
Unit.returnFun3 {} ()<caret> {}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun <T> T.returnFun5(fn: (T) -> Boolean): ((T) -> T) -> Unit = {}
fun test() {
25.returnFun5 { true } ()<caret> { it * 2 }
}
@@ -0,0 +1,13 @@
// IS_APPLICABLE: false
object A {
object B {
class C {
fun returnFun(fn: () -> Unit): (() -> Unit) -> Unit = {}
}
}
}
fun test() {
A.B.C().returnFun {} ()<caret> {}
}
@@ -12994,6 +12994,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda3.kt");
}
@TestMetadata("afterLambda4.kt")
public void testAfterLambda4() throws Exception {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda4.kt");
}
@TestMetadata("afterLambda5.kt")
public void testAfterLambda5() throws Exception {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda5.kt");
}
@TestMetadata("afterLambda6.kt")
public void testAfterLambda6() throws Exception {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda6.kt");
}
public void testAllFilesPresentInRemoveEmptyParenthesesFromLambdaCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}