diff --git a/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html b/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html new file mode 100644 index 00000000000..54ffaaedaee --- /dev/null +++ b/idea/resources/inspectionDescriptions/RemoveForLoopIndices.html @@ -0,0 +1,5 @@ + + +This inspection reports for loops iterating over a collection of values using "withIndex()" function with index variable not used in the loop body. + + diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template index 6c743b16877..a940c7a3e81 100644 --- a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/after.kt.template @@ -1,3 +1,3 @@ -for ((i, x) in foo.withIndices()) { +for ((i, x) in foo.withIndices()) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template index 8b1fae6e977..ef41e9fadce 100644 --- a/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template +++ b/idea/resources/intentionDescriptions/AddForLoopIndicesIntention/before.kt.template @@ -1,3 +1,3 @@ -for (x in foo) { +for (x in foo) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template index 8b1fae6e977..ef41e9fadce 100644 --- a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/after.kt.template @@ -1,3 +1,3 @@ -for (x in foo) { +for (x in foo) { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template index 6c743b16877..a940c7a3e81 100644 --- a/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template +++ b/idea/resources/intentionDescriptions/RemoveForLoopIndicesIntention/before.kt.template @@ -1,3 +1,3 @@ -for ((i, x) in foo.withIndices()) { +for ((i, x) in foo.withIndices()) { } \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4e29e4aeba4..5cfd263d0af 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1081,6 +1081,13 @@ cleanupTool="true" level="WARNING"/> + + diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt index 4495c4bed60..572b7e40062 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveForLoopIndicesIntention.kt @@ -16,20 +16,30 @@ package org.jetbrains.kotlin.idea.intentions +import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.search.searches.ReferencesSearch import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.editor.fixers.range +import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.psi.JetDotQualifiedExpression import org.jetbrains.kotlin.psi.JetForExpression import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.createExpressionByPattern -import org.jetbrains.kotlin.psi.psiUtil.endOffset -import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +public class RemoveForLoopIndicesInspection : IntentionBasedInspection( + listOf(IntentionBasedInspection.IntentionData(RemoveForLoopIndicesIntention())), + "Index is not used in the loop body", + javaClass() +) { + override val problemHighlightType: ProblemHighlightType + get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL +} + public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention(javaClass(), "Remove indices in for-loop") { private val WITH_INDEX_FQ_NAME = "kotlin.withIndex" @@ -46,7 +56,7 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention) : Int { - for ((i, c) in b.withIndex()) { + for ((i, c) in b.withIndex()) { return i } return 0 diff --git a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt index 22bccaea436..1c055d425a6 100644 --- a/idea/testData/intentions/removeForLoopIndices/loopWithType.kt +++ b/idea/testData/intentions/removeForLoopIndices/loopWithType.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: List) { - for ((i : Int, b: Int) in bar.withIndex()) { + for ((i : Int, b: Int) in bar.withIndex()) { } } \ No newline at end of file diff --git a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt index 067a31938eb..e907ccc131b 100644 --- a/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt +++ b/idea/testData/intentions/removeForLoopIndices/simpleLoopWithIndices.kt @@ -1,6 +1,6 @@ // WITH_RUNTIME fun foo(bar: List) { - for ((i,a) in bar.withIndex()) { + for ((i,a) in bar.withIndex()) { } } \ No newline at end of file