"Call on collection type may be reduced": fix false positive with mapNotNull, generic lambda block and new inference

#KT-32801 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-26 23:15:48 +09:00
committed by Yan Zhulanow
parent 6ee4b5e393
commit 4638a97bbc
3 changed files with 20 additions and 1 deletions
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.isFlexible
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
@@ -53,7 +54,9 @@ class UselessCallOnCollectionInspection : AbstractUselessCallInspection() {
if (TypeUtils.isNullableType(receiverTypeArgument)) return
if (calleeExpression.text != "filterNotNull") {
// Also check last argument functional type to have not-null result
if (!resolvedCall.hasLastFunctionalParameterWithResult(context) { !TypeUtils.isNullableType(it) }) return
if (!resolvedCall.hasLastFunctionalParameterWithResult(context) {
!TypeUtils.isNullableType(it) && it.constructor !is TypeVariableTypeConstructor
}) return
}
}
@@ -0,0 +1,11 @@
// PROBLEM: none
// COMPILER_ARGUMENTS: -XXLanguage:+NewInference
// WITH_RUNTIME
fun <R> mylet(block: () -> R): R = block()
val x = listOf(1, 2, 3).<caret>mapNotNull {
mylet {
null
}
}
@@ -1990,6 +1990,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/collections/uselessCallOnCollection/MapNotNullTo.kt");
}
@TestMetadata("MapNotNullWithGenericLambda.kt")
public void testMapNotNullWithGenericLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/uselessCallOnCollection/MapNotNullWithGenericLambda.kt");
}
@TestMetadata("MapNotNullWithLambda.kt")
public void testMapNotNullWithLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/uselessCallOnCollection/MapNotNullWithLambda.kt");