From 4ffeff5e6c3627163ca186125439ced3d390da59 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 24 Jan 2019 15:29:13 +0900 Subject: [PATCH] Redundant lambda arrow: do not report on type parameter #KT-28131 Fixed --- .../inspections/RedundantLambdaArrowInspection.kt | 10 ++++++++++ .../redundantLambdaArrow/typeParameter.kt | 6 ++++++ .../redundantLambdaArrow/typeParameter2.kt | 8 ++++++++ .../redundantLambdaArrow/typeParameter3.kt | 10 ++++++++++ .../inspections/LocalInspectionTestGenerated.java | 15 +++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter2.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter3.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt index 7d89a0336f7..afff0f7f0ca 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -15,12 +15,15 @@ import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore +import org.jetbrains.kotlin.types.typeUtil.isTypeParameter class RedundantLambdaArrowInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { @@ -49,6 +52,13 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() { if (context[BindingContext.EXPECTED_EXPRESSION_TYPE, lambdaExpression] == null) return } + val valueArgument = lambdaExpression.parent as? KtValueArgument + val valueArgumentCall = valueArgument?.getStrictParentOfType() + if (valueArgumentCall != null) { + val argumentMatch = valueArgumentCall.resolveToCall()?.getArgumentMapping(valueArgument) as? ArgumentMatch + if (argumentMatch?.valueParameter?.original?.type?.isTypeParameter() == true) return + } + val startOffset = functionLiteral.startOffset holder.registerProblem( holder.manager.createProblemDescriptor( diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter.kt new file mode 100644 index 00000000000..3f91844b6e2 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter.kt @@ -0,0 +1,6 @@ +// PROBLEM: none +fun foo(t: T) {} + +fun test() { + foo({ _: Boolean -> "" }) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter2.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter2.kt new file mode 100644 index 00000000000..70bc518e3f0 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter2.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +class Foo(val t: T) + +fun bar(foo: Foo<(Boolean) -> String>) {} + +fun test() { + bar(Foo({ _ -> "" })) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter3.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter3.kt new file mode 100644 index 00000000000..2f8cfcf772e --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter3.kt @@ -0,0 +1,10 @@ +// PROBLEM: none +// WITH_RUNTIME + +fun f(cbs: List<(Boolean) -> Unit>) { + cbs[0](true) +} + +fun main() { + f(listOf({ _ -> println("hello") })) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index a8eaa482b41..2c14f6e1559 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -4976,6 +4976,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt"); } + @TestMetadata("typeParameter.kt") + public void testTypeParameter() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter.kt"); + } + + @TestMetadata("typeParameter2.kt") + public void testTypeParameter2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter2.kt"); + } + + @TestMetadata("typeParameter3.kt") + public void testTypeParameter3() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/typeParameter3.kt"); + } + @TestMetadata("underscore.kt") public void testUnderscore() throws Exception { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt");