Check for type parameter supertypes in primitive numeric comparisons

This commit is contained in:
Dmitry Petrov
2018-02-09 12:40:52 +03:00
parent e5e583e3e5
commit 5a85bf36a5
4 changed files with 185 additions and 1 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtExpression
@@ -17,6 +18,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.*
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
class PrimitiveNumericComparisonInfo(
val comparisonType: KotlinType,
@@ -90,5 +92,15 @@ object PrimitiveNumericComparisonCallChecker : CallChecker {
}
private fun List<KotlinType>.findPrimitiveType() =
find { it.isPrimitiveNumberOrNullableType() }?.makeNotNullable()
firstNotNullResult { it.getPrimitiveTypeOrSupertype() }
private fun KotlinType.getPrimitiveTypeOrSupertype(): KotlinType? =
when {
constructor.declarationDescriptor is TypeParameterDescriptor ->
immediateSupertypes().firstNotNullResult { it.getPrimitiveTypeOrSupertype() }
isPrimitiveNumberOrNullableType() ->
makeNotNullable()
else ->
null
}
}