Unnecessary parentheses in function call with lambda: fix false positive

So #KT-23452 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-05-01 18:08:29 +03:00
committed by Mikhail Glukhikh
parent 8dbfd85d26
commit b8f3d588b7
4 changed files with 29 additions and 0 deletions
@@ -24,7 +24,9 @@ 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.KtValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
class RemoveEmptyParenthesesFromLambdaCallInspection : IntentionBasedInspection<KtValueArgumentList>(
RemoveEmptyParenthesesFromLambdaCallIntention::class
@@ -42,6 +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
val prevCall = element.getPrevSiblingIgnoringWhitespaceAndComments() as? KtCallExpression
if (prevCall?.lambdaArguments?.let { it.size > 0 } == true
|| prevCall?.valueArguments?.any { it.getArgumentExpression() is KtLambdaExpression } == true) return null
return element.range
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun returnFun(fn: () -> Unit): (() -> Unit) -> Unit = {}
fun test() {
returnFun {} ()<caret> {}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun returnFun(fn: () -> Unit, i: Int): (() -> Unit) -> Unit = {}
fun test() {
returnFun({}, 1)()<caret> {}
}
@@ -12522,6 +12522,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("afterLambda.kt")
public void testAfterLambda() throws Exception {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda.kt");
}
@TestMetadata("afterLambda2.kt")
public void testAfterLambda2() throws Exception {
runTest("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/afterLambda2.kt");
}
public void testAllFilesPresentInRemoveEmptyParenthesesFromLambdaCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}