Redundant arrow: don't report on forEach call with underscore
#KT-27209 Fixed
This commit is contained in:
@@ -14,10 +14,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import org.jetbrains.kotlin.KtNodeTypes
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
import org.jetbrains.kotlin.psi.KtContainerNodeForControlStructureBody
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
|
||||||
import org.jetbrains.kotlin.psi.KtWhenEntry
|
|
||||||
import org.jetbrains.kotlin.psi.lambdaExpressionVisitor
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
@@ -25,19 +22,25 @@ import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
|
|||||||
|
|
||||||
class RedundantLambdaArrowInspection : AbstractKotlinInspection() {
|
class RedundantLambdaArrowInspection : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
return lambdaExpressionVisitor { lambdaExpression ->
|
return lambdaExpressionVisitor(fun(lambdaExpression: KtLambdaExpression) {
|
||||||
val functionLiteral = lambdaExpression.functionLiteral
|
val functionLiteral = lambdaExpression.functionLiteral
|
||||||
val arrow = functionLiteral.arrow ?: return@lambdaExpressionVisitor
|
val arrow = functionLiteral.arrow ?: return
|
||||||
val parameters = functionLiteral.valueParameters
|
val parameters = functionLiteral.valueParameters
|
||||||
val singleParameter = parameters.singleOrNull()
|
val singleParameter = parameters.singleOrNull()
|
||||||
if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true && singleParameter?.name != "it") {
|
if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true && singleParameter?.name != "it") {
|
||||||
return@lambdaExpressionVisitor
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lambdaExpression.getStrictParentOfType<KtWhenEntry>()?.expression == lambdaExpression) return@lambdaExpressionVisitor
|
if (lambdaExpression.getStrictParentOfType<KtWhenEntry>()?.expression == lambdaExpression) return
|
||||||
if (lambdaExpression.getStrictParentOfType<KtContainerNodeForControlStructureBody>()?.let {
|
if (lambdaExpression.getStrictParentOfType<KtContainerNodeForControlStructureBody>()?.let {
|
||||||
it.node.elementType in listOf(KtNodeTypes.THEN, KtNodeTypes.ELSE) && it.expression == lambdaExpression
|
it.node.elementType in listOf(KtNodeTypes.THEN, KtNodeTypes.ELSE) && it.expression == lambdaExpression
|
||||||
} == true) return@lambdaExpressionVisitor
|
} == true) return
|
||||||
|
|
||||||
|
val callExpression = lambdaExpression.parent?.parent as? KtCallExpression
|
||||||
|
if (callExpression != null) {
|
||||||
|
val callee = callExpression.calleeExpression as? KtNameReferenceExpression
|
||||||
|
if (callee != null && callee.getReferencedName() == "forEach" && singleParameter?.name != "it") return
|
||||||
|
}
|
||||||
|
|
||||||
val startOffset = functionLiteral.startOffset
|
val startOffset = functionLiteral.startOffset
|
||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
@@ -50,7 +53,7 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() {
|
|||||||
DeleteFix()
|
DeleteFix()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeleteFix : LocalQuickFix {
|
class DeleteFix : LocalQuickFix {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
// We do not report "redundant arrow" here,
|
||||||
|
// because it's used to explicitly call forEach with unused lambda parameter
|
||||||
|
listOf(1, 2, 3).forEach { <caret>_ -> }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun println(s: String) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
listOf(1, 2, 3).forEach { <caret>it -> println(it) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun println(s: String) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
listOf(1, 2, 3).forEach { println(it) }
|
||||||
|
}
|
||||||
+10
@@ -4447,6 +4447,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantLambdaArrow"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantLambdaArrow"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("forEach.kt")
|
||||||
|
public void testForEach() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/forEach.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("forEachWithIt.kt")
|
||||||
|
public void testForEachWithIt() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/forEachWithIt.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("hasArguments.kt")
|
@TestMetadata("hasArguments.kt")
|
||||||
public void testHasArguments() throws Exception {
|
public void testHasArguments() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt");
|
runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user