提交 0b57d8eb 编写于 作者: S shiraji 提交者: Mikhail Glukhikh

Do not use "Remove redundant '.let' call" when receiver is used #KT-14390 Fixed

上级 6a1b0b9c
......@@ -17,12 +17,10 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) {
override fun inspectionTarget(element: KtCallExpression) = element.calleeExpression
......@@ -66,6 +64,9 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
val parameterName = lambdaExpression.getParameterName() ?: return false
val receiverExpression = dotQualifiedExpression.getLeftMostReceiverExpression()
if (receiverExpression.text != parameterName) return false
dotQualifiedExpression.selectorExpression?.let {
if (it.anyDescendantOfType<KtLambdaExpression>()) return false
}
return !dotQualifiedExpression.receiverUsedAsArgument(parameterName)
}
......@@ -79,7 +80,15 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
}
private fun KtDotQualifiedExpression.receiverUsedAsArgument(receiverName: String): Boolean {
if ((selectorExpression as? KtCallExpression)?.valueArguments?.firstOrNull { it.text == receiverName } != null) return true
(selectorExpression as? KtCallExpression)?.valueArguments?.let {
if (it.any { it.receiverUsedAsArgument(receiverName) }) return true
}
return (receiverExpression as? KtDotQualifiedExpression)?.receiverUsedAsArgument(receiverName) ?: false
}
private fun KtValueArgument.receiverUsedAsArgument(receiverName: String) =
text == receiverName || anyDescendantOfType<KtDotQualifiedExpression> {
it.getLeftMostReceiverExpression().text == receiverName ||
it.receiverUsedAsArgument(receiverName)
}
}
\ No newline at end of file
// WITH_RUNTIME
// IS_APPLICABLE: false
fun baz(foo: String) {
foo.let<caret> { it.substringAfterLast(it.capitalize()) }
}
// WITH_RUNTIME
// IS_APPLICABLE: false
fun baz(foo: String) {
foo.let<caret> { it.substringAfterLast("".equals(it).toString()) }
}
// WITH_RUNTIME
// IS_APPLICABLE: false
fun baz(foo: String) {
foo.let<caret> { it.substring(0, it.length) }
}
// WITH_RUNTIME
// IS_APPLICABLE: false
fun baz(foo: String) {
foo.let<caret> { it.indexOfLast { c -> c == it[0] } }
}
\ No newline at end of file
// WITH_RUNTIME
// IS_APPLICABLE: false
// This should be reported. However, in order to avoid too complecate logic, the intention ignore this case.
import java.util.*
fun baz2(foo: List<String>) {
foo.let<caret> { it.binarySearch("", Comparator<kotlin.String> { o1, o2 -> 0 }) }
}
\ No newline at end of file
......@@ -11768,6 +11768,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("multipleReceiver.kt")
public void testMultipleReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/multipleReceiver.kt");
doTest(fileName);
}
@TestMetadata("multipleReceiver2.kt")
public void testMultipleReceiver2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/multipleReceiver2.kt");
doTest(fileName);
}
@TestMetadata("multipleReceiver3.kt")
public void testMultipleReceiver3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/multipleReceiver3.kt");
doTest(fileName);
}
@TestMetadata("receiverWithLambda.kt")
public void testReceiverWithLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/receiverWithLambda.kt");
doTest(fileName);
}
@TestMetadata("receiverWithLambda2.kt")
public void testReceiverWithLambda2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/receiverWithLambda2.kt");
doTest(fileName);
}
@TestMetadata("sameLets.kt")
public void testSameLets() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/sameLets.kt");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册