KT-13482 "Loop can be replaced with stdlib operations" changes semantics
#KT-13482 Fixed
This commit is contained in:
+8
-3
@@ -17,9 +17,9 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
|
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
|
||||||
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
||||||
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtPsiUtil.isAssignment
|
||||||
import org.jetbrains.kotlin.psi.KtForExpression
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
|
|
||||||
class ForEachTransformation(
|
class ForEachTransformation(
|
||||||
loop: KtForExpression,
|
loop: KtForExpression,
|
||||||
@@ -53,6 +53,11 @@ class ForEachTransformation(
|
|||||||
if (state.previousTransformations.isEmpty()) return null // do not suggest conversion to just ".forEach{}" or ".forEachIndexed{}"
|
if (state.previousTransformations.isEmpty()) return null // do not suggest conversion to just ".forEach{}" or ".forEachIndexed{}"
|
||||||
|
|
||||||
val statement = state.statements.singleOrNull() ?: return null
|
val statement = state.statements.singleOrNull() ?: return null
|
||||||
|
|
||||||
|
// check if contains assignment to non-qualified variable - in this case only use of lazy sequence is correct
|
||||||
|
if (!state.lazySequence
|
||||||
|
&& statement.anyDescendantOfType<KtBinaryExpression> { isAssignment(it) && it.left is KtNameReferenceExpression }) return null
|
||||||
|
|
||||||
//TODO: should we disallow it for complicated statements like loops, if, when?
|
//TODO: should we disallow it for complicated statements like loops, if, when?
|
||||||
val transformation = ForEachTransformation(state.outerLoop, state.inputVariable, state.indexVariable, statement)
|
val transformation = ForEachTransformation(state.outerLoop, state.inputVariable, state.indexVariable, statement)
|
||||||
return TransformationMatch.Result(transformation)
|
return TransformationMatch.Result(transformation)
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
var max_width = 0.0f
|
||||||
|
<caret>for (i in 0..lineCount - 1) {
|
||||||
|
if (getLineWidth(i) > max_width) {
|
||||||
|
max_width = getLineWidth(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
|
||||||
|
fun getMaxLineWidth(lineCount: Int): Float {
|
||||||
|
var max_width = 0.0f
|
||||||
|
<caret>(0..lineCount - 1)
|
||||||
|
.asSequence()
|
||||||
|
.filter { getLineWidth(it) > max_width }
|
||||||
|
.forEach { max_width = getLineWidth(it) }
|
||||||
|
return max_width
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLineWidth(i: Int): Float = TODO()
|
||||||
@@ -131,6 +131,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asSequenceOnly.kt")
|
||||||
|
public void testAsSequenceOnly() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/asSequenceOnly.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("assignFilter.kt")
|
@TestMetadata("assignFilter.kt")
|
||||||
public void testAssignFilter() throws Exception {
|
public void testAssignFilter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/assignFilter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/assignFilter.kt");
|
||||||
|
|||||||
@@ -7291,6 +7291,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("asSequenceOnly.kt")
|
||||||
|
public void testAsSequenceOnly() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/asSequenceOnly.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("assignFilter.kt")
|
@TestMetadata("assignFilter.kt")
|
||||||
public void testAssignFilter() throws Exception {
|
public void testAssignFilter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/assignFilter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/assignFilter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user