Skip to content

Commit

Permalink
Support 'it op something' case in "redundant let" #KT-21373 Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
shiraji authored and mglukhikh committed Dec 5, 2017
1 parent f961f33 commit 5d44037
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 11 deletions.
Expand Up @@ -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) =
Expand Down
6 changes: 6 additions & 0 deletions idea/testData/intentions/replaceSingleLineLetIntention/in.kt
@@ -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) }
}
@@ -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) } }
}
Expand Up @@ -14174,6 +14174,30 @@ public void testDotWithComparison() throws Exception {
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");
Expand Down

0 comments on commit 5d44037

Please sign in to comment.