Added inspection based on RemoveForLoopIndicesIntention

This commit is contained in:
Valentin Kipyatkov
2015-07-21 14:04:51 +03:00
parent 6b0fc8391f
commit 6339ad4ec6
10 changed files with 32 additions and 10 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports for loops iterating over a collection of values using "withIndex()" function with index variable not used in the loop body.
</body>
</html>
@@ -1,3 +1,3 @@
for ((i, x) in foo.withIndices()) {
for (<spot>(i, x) in foo.withIndices()</spot>) {
}
@@ -1,3 +1,3 @@
for (x in foo) {
for (<spot>x in foo</spot>) {
}
@@ -1,3 +1,3 @@
for (x in foo) {
for (<spot>x in foo</spot>) {
}
@@ -1,3 +1,3 @@
for ((i, x) in foo.withIndices()) {
for (<spot>(i, x) in foo.withIndices()</spot>) {
}
+7
View File
@@ -1081,6 +1081,13 @@
cleanupTool="true"
level="WARNING"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveForLoopIndicesInspection"
displayName="Index is unused"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
/>
<project.converterProvider implementation="org.jetbrains.kotlin.idea.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -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<JetForExpression>(
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<JetForExpression>(javaClass(), "Remove indices in for-loop") {
private val WITH_INDEX_FQ_NAME = "kotlin.withIndex"
@@ -46,7 +56,7 @@ public class RemoveForLoopIndicesIntention : JetSelfTargetingRangeIntention<JetF
val indexVar = multiParameter.entries[0]
if (ReferencesSearch.search(indexVar).any()) return null
return TextRange(element.startOffset, element.body?.startOffset ?: element.endOffset)
return indexVar.nameIdentifier?.range
}
override fun applyTo(element: JetForExpression, editor: Editor) {
@@ -2,7 +2,7 @@
// WITH_RUNTIME
fun foo(b: List<Int>) : Int {
for ((i, <caret>c) in b.withIndex()) {
for ((<caret>i, c) in b.withIndex()) {
return i
}
return 0
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: List<Int>) {
for ((i : <caret>Int, b: Int) in bar.withIndex()) {
for ((i<caret> : Int, b: Int) in bar.withIndex()) {
}
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
fun foo(bar: List<String>) {
for ((i,<caret>a) in bar.withIndex()) {
for ((i<caret>,a) in bar.withIndex()) {
}
}