From a87ca8432e9d724e1a3f84751597f7354835b843 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 15 Oct 2016 23:17:50 +0300 Subject: [PATCH] KT-14341 for to stdlib to forEach: if the loop variable is not used in the last/innerest statement the conversion is not proposed #KT-14341 Fixed --- .../loopToCallChain/result/ForEachTransformation.kt | 3 +++ .../testData/intentions/loopToCallChain/count/long.kt | 4 ++-- .../intentions/loopToCallChain/count/long.kt.after | 10 ++++++++++ .../intentions/loopToCallChain/count/long.kt.after2 | 11 +++++++++++ .../loopToCallChain/count/variableUsedBefore.kt | 4 ++-- .../loopToCallChain/count/variableUsedBefore.kt.after | 10 ++++++++++ .../count/variableUsedBefore.kt.after2 | 11 +++++++++++ .../intentions/loopToCallChain/forEach/KT14341.kt | 9 +++++++++ .../loopToCallChain/forEach/KT14341.kt.after | 8 ++++++++ .../loopToCallChain/forEach/KT14341.kt.after2 | 9 +++++++++ .../loopToCallChain/map/mapTo_inputVarNotUsed.kt | 4 ++-- .../map/mapTo_inputVarNotUsed.kt.after | 8 ++++++++ .../map/mapTo_inputVarNotUsed.kt.after2 | 9 +++++++++ .../idea/intentions/IntentionTest2Generated.java | 6 ++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 15 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 idea/testData/intentions/loopToCallChain/count/long.kt.after create mode 100644 idea/testData/intentions/loopToCallChain/count/long.kt.after2 create mode 100644 idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after create mode 100644 idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after2 create mode 100644 idea/testData/intentions/loopToCallChain/forEach/KT14341.kt create mode 100644 idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after create mode 100644 idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after2 create mode 100644 idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after create mode 100644 idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after2 diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt index 2868420ead5..08799423f56 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt @@ -49,6 +49,9 @@ class ForEachTransformation( override val indexVariableAllowed: Boolean get() = true + override val shouldUseInputVariables: Boolean + get() = false + override fun match(state: MatchingState): TransformationMatch.Result? { if (state.previousTransformations.isEmpty()) return null // do not suggest conversion to just ".forEach{}" or ".forEachIndexed{}" diff --git a/idea/testData/intentions/loopToCallChain/count/long.kt b/idea/testData/intentions/loopToCallChain/count/long.kt index 39257ca84c8..3caed403dce 100644 --- a/idea/testData/intentions/loopToCallChain/count/long.kt +++ b/idea/testData/intentions/loopToCallChain/count/long.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME -// IS_APPLICABLE: false -// IS_APPLICABLE_2: false +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" fun foo(list: List): Long { var count = 0L for (s in list) { diff --git a/idea/testData/intentions/loopToCallChain/count/long.kt.after b/idea/testData/intentions/loopToCallChain/count/long.kt.after new file mode 100644 index 00000000000..e1b9db4157c --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/count/long.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List): Long { + var count = 0L + list + .filter { it.length > 10 } + .forEach { count++ } + return count +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/count/long.kt.after2 b/idea/testData/intentions/loopToCallChain/count/long.kt.after2 new file mode 100644 index 00000000000..bbe485c5103 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/count/long.kt.after2 @@ -0,0 +1,11 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List): Long { + var count = 0L + list + .asSequence() + .filter { it.length > 10 } + .forEach { count++ } + return count +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt index bbeafe77d30..518d0e76879 100644 --- a/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt +++ b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME -// IS_APPLICABLE: false -// IS_APPLICABLE_2: false +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" fun foo(list: List): Int { var count = 0 for (s in list) { diff --git a/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after new file mode 100644 index 00000000000..2da9e9c874f --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List): Int { + var count = 0 + list + .filter { it.length > count } + .forEach { count++ } + return count +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after2 b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after2 new file mode 100644 index 00000000000..172d29a0859 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/count/variableUsedBefore.kt.after2 @@ -0,0 +1,11 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List): Int { + var count = 0 + list + .asSequence() + .filter { it.length > count } + .forEach { count++ } + return count +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt new file mode 100644 index 00000000000..d49de0dadc0 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List){ + for (any in list) { + if (any > 0) + println("positive number") + } +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after new file mode 100644 index 00000000000..800a54f6d15 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List){ + list + .filter { it > 0 } + .forEach { println("positive number") } +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after2 b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after2 new file mode 100644 index 00000000000..500b76eb22e --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/forEach/KT14341.kt.after2 @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List){ + list + .asSequence() + .filter { it > 0 } + .forEach { println("positive number") } +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt index 58ad9c221d1..b78bed39ab7 100644 --- a/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt +++ b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME -// IS_APPLICABLE: false -// IS_APPLICABLE_2: false +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" fun foo(list: List, target: MutableList) { for (s in list) { if (s.length > 0) diff --git a/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after new file mode 100644 index 00000000000..a4b0c672c8e --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List, target: MutableList) { + list + .filter { it.length > 0 } + .forEach { target.add(0) } +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after2 b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after2 new file mode 100644 index 00000000000..adbcd7db504 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/map/mapTo_inputVarNotUsed.kt.after2 @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'" +// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'" +fun foo(list: List, target: MutableList) { + list + .asSequence() + .filter { it.length > 0 } + .forEach { target.add(0) } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java index 312fda26a2a..c528f34d568 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java @@ -781,6 +781,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 { doTest(fileName); } + @TestMetadata("KT14341.kt") + public void testKT14341() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/forEach/KT14341.kt"); + doTest(fileName); + } + @TestMetadata("notAvailable.kt") public void testNotAvailable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/forEach/notAvailable.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index bd396b60cf8..2efeb0a3008 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8689,6 +8689,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("KT14341.kt") + public void testKT14341() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/forEach/KT14341.kt"); + doTest(fileName); + } + @TestMetadata("notAvailable.kt") public void testNotAvailable() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/forEach/notAvailable.kt");