Does not replace explicit lambda parameter with 'it' if conflicting nested literal available #KT-11849 Fixed
This commit is contained in:
+10
@@ -30,6 +30,8 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
|||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
||||||
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
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.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||||
@@ -44,6 +46,14 @@ class ReplaceExplicitFunctionLiteralParamWithItIntention() : PsiElementBaseInten
|
|||||||
val parameter = functionLiteral.valueParameters.singleOrNull() ?: return false
|
val parameter = functionLiteral.valueParameters.singleOrNull() ?: return false
|
||||||
if (parameter.typeReference != null) 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'"
|
text = "Replace explicit parameter '${parameter.name}' with 'it'"
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
+10
@@ -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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+10
@@ -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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Vendored
+12
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+10
@@ -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);
|
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")
|
@TestMetadata("notApplicable_alreadyUsesImplicitIt.kt")
|
||||||
public void testNotApplicable_alreadyUsesImplicitIt() throws Exception {
|
public void testNotApplicable_alreadyUsesImplicitIt() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_alreadyUsesImplicitIt.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_alreadyUsesImplicitIt.kt");
|
||||||
@@ -7945,6 +7951,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("notApplicable_notFunctionLiteralParameter.kt")
|
||||||
public void testNotApplicable_notFunctionLiteralParameter() throws Exception {
|
public void testNotApplicable_notFunctionLiteralParameter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_notFunctionLiteralParameter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceExplicitFunctionLiteralParamWithIt/notApplicable_notFunctionLiteralParameter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user