Do not use "Remove redundant '.let' call" when receiver is used #KT-14390 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
6a1b0b9cbc
commit
0b57d8eb49
@@ -17,12 +17,10 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
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.idea.inspections.IntentionBasedInspection
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
|
||||||
|
|
||||||
class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) {
|
class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) {
|
||||||
override fun inspectionTarget(element: KtCallExpression) = element.calleeExpression
|
override fun inspectionTarget(element: KtCallExpression) = element.calleeExpression
|
||||||
@@ -66,6 +64,9 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
|
|||||||
val parameterName = lambdaExpression.getParameterName() ?: return false
|
val parameterName = lambdaExpression.getParameterName() ?: return false
|
||||||
val receiverExpression = dotQualifiedExpression.getLeftMostReceiverExpression()
|
val receiverExpression = dotQualifiedExpression.getLeftMostReceiverExpression()
|
||||||
if (receiverExpression.text != parameterName) return false
|
if (receiverExpression.text != parameterName) return false
|
||||||
|
dotQualifiedExpression.selectorExpression?.let {
|
||||||
|
if (it.anyDescendantOfType<KtLambdaExpression>()) return false
|
||||||
|
}
|
||||||
return !dotQualifiedExpression.receiverUsedAsArgument(parameterName)
|
return !dotQualifiedExpression.receiverUsedAsArgument(parameterName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +80,15 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun KtDotQualifiedExpression.receiverUsedAsArgument(receiverName: String): Boolean {
|
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
|
return (receiverExpression as? KtDotQualifiedExpression)?.receiverUsedAsArgument(receiverName) ?: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtValueArgument.receiverUsedAsArgument(receiverName: String) =
|
||||||
|
text == receiverName || anyDescendantOfType<KtDotQualifiedExpression> {
|
||||||
|
it.getLeftMostReceiverExpression().text == receiverName ||
|
||||||
|
it.receiverUsedAsArgument(receiverName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun baz(foo: String) {
|
||||||
|
foo.let<caret> { it.substringAfterLast(it.capitalize()) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun baz(foo: String) {
|
||||||
|
foo.let<caret> { it.substringAfterLast("".equals(it).toString()) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun baz(foo: String) {
|
||||||
|
foo.let<caret> { it.substring(0, it.length) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun baz(foo: String) {
|
||||||
|
foo.let<caret> { it.indexOfLast { c -> c == it[0] } }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// 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 }) }
|
||||||
|
}
|
||||||
@@ -11768,6 +11768,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("sameLets.kt")
|
||||||
public void testSameLets() throws Exception {
|
public void testSameLets() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/sameLets.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/sameLets.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user