Redundant lambda arrow: do not report on type parameter

#KT-28131 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-01-24 15:29:13 +09:00
committed by Mikhail Glukhikh
parent 8fbf1ed683
commit 4ffeff5e6c
5 changed files with 49 additions and 0 deletions
@@ -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<KtCallExpression>()
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(
@@ -0,0 +1,6 @@
// PROBLEM: none
fun <T> foo(t: T) {}
fun test() {
foo({ <caret>_: Boolean -> "" })
}
@@ -0,0 +1,8 @@
// PROBLEM: none
class Foo<T>(val t: T)
fun bar(foo: Foo<(Boolean) -> String>) {}
fun test() {
bar(Foo({ <caret>_ -> "" }))
}
@@ -0,0 +1,10 @@
// PROBLEM: none
// WITH_RUNTIME
fun f(cbs: List<(Boolean) -> Unit>) {
cbs[0](true)
}
fun main() {
f(listOf({ <caret>_ -> println("hello") }))
}
@@ -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");