Does not replace explicit lambda parameter with 'it' if conflicting nested literal available #KT-11849 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-04-15 17:35:08 +03:00
parent 2e1a2fa8d1
commit 3f266647f0
6 changed files with 70 additions and 0 deletions
@@ -30,6 +30,8 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -44,6 +46,14 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseInten
val parameter = functionLiteral.valueParameters.singleOrNull() ?: return false
if (parameter.typeReference != null) return false
if (functionLiteral.anyDescendantOfType<KtFunctionLiteral>() { literal ->
literal !== functionLiteral &&
!literal.hasParameterSpecification() &&
literal.anyDescendantOfType<KtSimpleNameExpression> { nameExpr ->
nameExpr.getReferencedName() == element.text
}
} ) return false
text = "Replace explicit parameter '${parameter.name}' with 'it'"
return true
}
@@ -0,0 +1,10 @@
// IS_APPLICABLE: true
inline fun <T, R> T.let(block: (T) -> R): R = block(this)
fun foo(arg: Any?, y: Any?): Any? {
return arg?.let {
<caret>x -> x.toString().let { y }
}
}
@@ -0,0 +1,10 @@
// IS_APPLICABLE: true
inline fun <T, R> T.let(block: (T) -> R): R = block(this)
fun foo(arg: Any?, y: Any?): Any? {
return arg?.let {
it.toString().let { y }
}
}
@@ -0,0 +1,12 @@
// IS_APPLICABLE: false
inline fun <T, R> T.let(block: (T) -> R): R = block(this)
fun foo(arg: Any?): Int? {
return arg?.let {
<caret>x -> x.toString().let {
x.hashCode() + it.hashCode()
}
}
}
@@ -0,0 +1,10 @@
// IS_APPLICABLE: false
inline fun <T, R> T.let(block: (T) -> R): R = block(this)
fun foo(arg: Any?): Any? {
return arg?.let {
<caret>x -> x.toString().let { x }
}
}
@@ -7933,6 +7933,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("applicable_nestedLiteralsNoUseInside.kt")
public void testApplicable_nestedLiteralsNoUseInside() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/applicable_nestedLiteralsNoUseInside.kt");
doTest(fileName);
}
@TestMetadata("notApplicable_alreadyUsesImplicitIt.kt")
public void testNotApplicable_alreadyUsesImplicitIt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_alreadyUsesImplicitIt.kt");
@@ -7945,6 +7951,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("notApplicable_nestedLiterals.kt")
public void testNotApplicable_nestedLiterals() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiterals.kt");
doTest(fileName);
}
@TestMetadata("notApplicable_nestedLiteralsNoIt.kt")
public void testNotApplicable_nestedLiteralsNoIt() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_nestedLiteralsNoIt.kt");
doTest(fileName);
}
@TestMetadata("notApplicable_notFunctionLiteralParameter.kt")
public void testNotApplicable_notFunctionLiteralParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_notFunctionLiteralParameter.kt");