Support 'it op something' case in "redundant let" #KT-21373 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
f961f33f9d
commit
5d44037a2b
@@ -90,21 +90,35 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<Kt
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtBinaryExpression.isApplicable(parameterName: String): Boolean {
|
||||
private fun KtBinaryExpression.isApplicable(parameterName: String, isTopLevel: Boolean = true): Boolean {
|
||||
val left = left ?: return false
|
||||
when (left) {
|
||||
is KtNameReferenceExpression -> if (left.text != parameterName) return false
|
||||
is KtDotQualifiedExpression -> if (!left.isApplicable(parameterName)) return false
|
||||
else -> return false
|
||||
if (isTopLevel) {
|
||||
when (left) {
|
||||
is KtNameReferenceExpression -> if (left.text != parameterName) return false
|
||||
is KtDotQualifiedExpression -> if (!left.isApplicable(parameterName)) return false
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!left.isApplicable(parameterName)) return false
|
||||
}
|
||||
|
||||
val right = right ?: return false
|
||||
return when (right) {
|
||||
is KtNameReferenceExpression -> right.text != parameterName
|
||||
is KtDotQualifiedExpression -> !right.hasLambdaExpression() && !right.nameUsed(parameterName)
|
||||
is KtConstantExpression -> true
|
||||
else -> false
|
||||
}
|
||||
return right.isApplicable(parameterName)
|
||||
}
|
||||
|
||||
private fun KtExpression.isApplicable(parameterName: String): Boolean = when (this) {
|
||||
is KtNameReferenceExpression -> text != parameterName
|
||||
is KtDotQualifiedExpression -> !hasLambdaExpression() && !nameUsed(parameterName)
|
||||
is KtBinaryExpression -> isApplicable(parameterName, isTopLevel = false)
|
||||
is KtCallExpression -> isApplicable(parameterName)
|
||||
is KtConstantExpression -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
private fun KtCallExpression.isApplicable(parameterName: String): Boolean = valueArguments.all {
|
||||
val argumentExpression = it.getArgumentExpression() ?: return@all false
|
||||
argumentExpression.isApplicable(parameterName)
|
||||
}
|
||||
|
||||
private fun KtDotQualifiedExpression.isApplicable(parameterName: String) =
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it.let<caret> { it in 1000..3000 } }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it in 1000..3000 }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it.let<caret> { value -> value in value..3000 } }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it.let<caret> { it in IntRange(1, 10) } }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it in IntRange(1, 10) }
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo(list: List<Int>) {
|
||||
list.filter { it.let<caret> { it in IntRange(it - 1, 10) } }
|
||||
}
|
||||
@@ -14174,6 +14174,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("in.kt")
|
||||
public void testIn() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/in.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inWithMultipleParam.kt")
|
||||
public void testInWithMultipleParam() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/inWithMultipleParam.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inWithRange.kt")
|
||||
public void testInWithRange() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/inWithRange.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inWithRangeMultipleParam.kt")
|
||||
public void testInWithRangeMultipleParam() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/inWithRangeMultipleParam.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("lambdaWithBinaryExpression.kt")
|
||||
public void testLambdaWithBinaryExpression() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/lambdaWithBinaryExpression.kt");
|
||||
|
||||
Reference in New Issue
Block a user