summaryrefslogtreecommitdiff
path: root/idea
diff options
context:
space:
mode:
authorToshiaki Kameyama <kmymtsak@gmail.com>2021-01-06 15:03:36 +0100
committerkotlin-ide-monorepo-bot <kotlin-ide-monorepo-bot-no-reply@jetbrains.com>2021-01-06 14:03:36 +0000
commita9af7787fb080a2a3832bc30c7f7725d71c3d0b6 (patch)
tree10f115f0522e8e01c02f6f951153305a1ead434d /idea
parentfaa629e2e5f0e16d015a47b3a3256ef49bf273ff (diff)
downloadintellij-kotlin-a9af7787fb080a2a3832bc30c7f7725d71c3d0b6.tar.gz
Redundant 'asSequence' call don't report when grand parent is collection function call
#KT-43930 Fixed GitOrigin-RevId: 465d9b13f6b280033b947031d67af17ae7ac9f05
Diffstat (limited to 'idea')
-rw-r--r--idea/src/org/jetbrains/kotlin/idea/inspections/collections/ConvertCallChainIntoSequenceInspection.kt8
-rw-r--r--idea/src/org/jetbrains/kotlin/idea/inspections/collections/RedundantAsSequenceInspection.kt12
-rw-r--r--idea/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java5
-rw-r--r--idea/testData/inspectionsLocal/collections/redundantAsSequence/inCallChain.kt5
-rw-r--r--idea/testData/inspectionsLocal/collections/redundantAsSequence/notTermination.kt2
5 files changed, 27 insertions, 5 deletions
diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/ConvertCallChainIntoSequenceInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/ConvertCallChainIntoSequenceInspection.kt
index 85f5f564e8f7..fdf93b81329d 100644
--- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/ConvertCallChainIntoSequenceInspection.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/ConvertCallChainIntoSequenceInspection.kt
@@ -200,8 +200,7 @@ private fun KtCallExpression.isLazyTermination(context: BindingContext): Boolean
return isCalling(fqName, context)
}
-@NonNls
-private val transformations = listOf(
+internal val collectionTransformationFunctionNames = listOf(
"chunked",
"distinct",
"distinctBy",
@@ -234,7 +233,10 @@ private val transformations = listOf(
"windowed",
"withIndex",
"zipWithNext"
-).associateWith { FqName("kotlin.collections.$it") }
+)
+
+@NonNls
+private val transformations = collectionTransformationFunctionNames.associateWith { FqName("kotlin.collections.$it") }
internal val collectionTerminationFunctionNames = listOf(
"all",
diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/RedundantAsSequenceInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/RedundantAsSequenceInspection.kt
index 66d86bef9da7..6414b3e5cb34 100644
--- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/RedundantAsSequenceInspection.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/RedundantAsSequenceInspection.kt
@@ -31,19 +31,24 @@ class RedundantAsSequenceInspection : AbstractKotlinInspection() {
companion object {
private val asSequenceFqName = FqName("kotlin.collections.asSequence")
private val terminations = collectionTerminationFunctionNames.associateWith { FqName("kotlin.sequences.$it") }
+ private val transformationsAndTerminations =
+ collectionTransformationFunctionNames.associateWith { FqName("kotlin.sequences.$it") } + terminations
}
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = qualifiedExpressionVisitor(fun(qualified) {
val call = qualified.callExpression ?: return
val callee = call.calleeExpression ?: return
if (callee.text != "asSequence") return
- val parentCall = qualified.getQualifiedExpressionForReceiver()?.callExpression ?: return
+ val parent = qualified.getQualifiedExpressionForReceiver()
+ val parentCall = parent?.callExpression ?: return
val context = qualified.analyze(BodyResolveMode.PARTIAL)
val receiverType = qualified.receiverExpression.getType(context) ?: return
if (!receiverType.isIterable(DefaultBuiltIns.Instance)) return
if (call.getResolvedCall(context)?.isCalling(asSequenceFqName) != true) return
if (!parentCall.isTermination(context)) return
+ val grandParentCall = parent.getQualifiedExpressionForReceiver()?.callExpression
+ if (grandParentCall?.isTransformationOrTermination(context) == true) return
holder.registerProblem(
qualified,
@@ -58,6 +63,11 @@ class RedundantAsSequenceInspection : AbstractKotlinInspection() {
return isCalling(fqName, context)
}
+ private fun KtCallExpression.isTransformationOrTermination(context: BindingContext): Boolean {
+ val fqName = transformationsAndTerminations[calleeExpression?.text] ?: return false
+ return isCalling(fqName, context)
+ }
+
private class RemoveAsSequenceFix : LocalQuickFix {
override fun getName() = KotlinBundle.message("remove.assequence.call.fix.text")
diff --git a/idea/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java
index 17c79d95ca07..8a9e218d617c 100644
--- a/idea/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java
+++ b/idea/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java
@@ -1521,6 +1521,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/collections/redundantAsSequence/hasLineBreak.kt");
}
+ @TestMetadata("inCallChain.kt")
+ public void testInCallChain() throws Exception {
+ runTest("testData/inspectionsLocal/collections/redundantAsSequence/inCallChain.kt");
+ }
+
@TestMetadata("map.kt")
public void testMap() throws Exception {
runTest("testData/inspectionsLocal/collections/redundantAsSequence/map.kt");
diff --git a/idea/testData/inspectionsLocal/collections/redundantAsSequence/inCallChain.kt b/idea/testData/inspectionsLocal/collections/redundantAsSequence/inCallChain.kt
new file mode 100644
index 000000000000..6710149c6b87
--- /dev/null
+++ b/idea/testData/inspectionsLocal/collections/redundantAsSequence/inCallChain.kt
@@ -0,0 +1,5 @@
+// PROBLEM: none
+// WITH_RUNTIME
+fun test(list: List<Int>) {
+ list.<caret>asSequence().runningReduce { acc, i -> acc + i }.take(10).last()
+} \ No newline at end of file
diff --git a/idea/testData/inspectionsLocal/collections/redundantAsSequence/notTermination.kt b/idea/testData/inspectionsLocal/collections/redundantAsSequence/notTermination.kt
index 665a212bed4a..f47469ab2951 100644
--- a/idea/testData/inspectionsLocal/collections/redundantAsSequence/notTermination.kt
+++ b/idea/testData/inspectionsLocal/collections/redundantAsSequence/notTermination.kt
@@ -1,5 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<String>) {
- list.<caret>sorted().first()
+ list.<caret>asSequence().sorted().first()
}