BindingContextUtils: fix warnings

This commit is contained in:
Mikhail Glukhikh
2019-07-09 13:03:27 +03:00
parent b97f187cf6
commit b24b8bfb28
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.resolve.scopes.utils.takeSnapshot
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice
fun KtReturnExpression.getTargetFunctionDescriptor(context: BindingContext): FunctionDescriptor? {
val targetLabel = getTargetLabel()
@@ -43,7 +42,7 @@ fun KtReturnExpression.getTargetFunctionDescriptor(context: BindingContext): Fun
val declarationDescriptor = context[DECLARATION_TO_DESCRIPTOR, getNonStrictParentOfType<KtDeclarationWithBody>()]
val containingFunctionDescriptor = DescriptorUtils.getParentOfType(declarationDescriptor, FunctionDescriptor::class.java, false)
if (containingFunctionDescriptor == null) return null
?: return null
return generateSequence(containingFunctionDescriptor) { DescriptorUtils.getParentOfType(it, FunctionDescriptor::class.java) }
.dropWhile { it is AnonymousFunctionDescriptor }
@@ -54,26 +53,26 @@ fun KtReturnExpression.getTargetFunction(context: BindingContext): KtCallableDec
return getTargetFunctionDescriptor(context)?.let { DescriptorToSourceUtils.descriptorToDeclaration(it) as? KtCallableDeclaration }
}
fun KtExpression.isUsedAsExpression(context: BindingContext): Boolean = context[BindingContext.USED_AS_EXPRESSION, this]!!
fun KtExpression.isUsedAsResultOfLambda(context: BindingContext): Boolean = context[BindingContext.USED_AS_RESULT_OF_LAMBDA, this]!!
fun KtExpression.isUsedAsExpression(context: BindingContext): Boolean = context[USED_AS_EXPRESSION, this]!!
fun KtExpression.isUsedAsResultOfLambda(context: BindingContext): Boolean = context[USED_AS_RESULT_OF_LAMBDA, this]!!
fun KtExpression.isUsedAsStatement(context: BindingContext): Boolean = !isUsedAsExpression(context)
fun <C : ResolutionContext<C>> ResolutionContext<C>.recordDataFlowInfo(expression: KtExpression?) {
if (expression == null) return
val typeInfo = trace.get(BindingContext.EXPRESSION_TYPE_INFO, expression)
val typeInfo = trace.get(EXPRESSION_TYPE_INFO, expression)
if (typeInfo != null) {
trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo.replaceDataFlowInfo(dataFlowInfo))
trace.record(EXPRESSION_TYPE_INFO, expression, typeInfo.replaceDataFlowInfo(dataFlowInfo))
} else if (dataFlowInfo != DataFlowInfo.EMPTY) {
// Don't store anything in BindingTrace if it's simply an empty DataFlowInfo
trace.record(BindingContext.EXPRESSION_TYPE_INFO, expression, noTypeInfo(dataFlowInfo))
trace.record(EXPRESSION_TYPE_INFO, expression, noTypeInfo(dataFlowInfo))
}
}
fun BindingTrace.recordScope(scope: LexicalScope, element: KtElement?) {
if (element != null) {
record(BindingContext.LEXICAL_SCOPE, element, scope.takeSnapshot() as LexicalScope)
record(LEXICAL_SCOPE, element, scope.takeSnapshot() as LexicalScope)
}
}
@@ -83,7 +82,7 @@ fun BindingContext.getDataFlowInfoAfter(position: PsiElement): DataFlowInfo {
val parent = it.parent
//TODO: it's a hack because KotlinTypeInfo with wrong DataFlowInfo stored for call expression after qualifier
if (parent is KtQualifiedExpression && it == parent.selectorExpression) return@let null
this[BindingContext.EXPRESSION_TYPE_INFO, it]
this[EXPRESSION_TYPE_INFO, it]
}?.let { return it.dataFlowInfo }
}
return DataFlowInfo.EMPTY
@@ -92,25 +91,22 @@ fun BindingContext.getDataFlowInfoAfter(position: PsiElement): DataFlowInfo {
fun BindingContext.getDataFlowInfoBefore(position: PsiElement): DataFlowInfo {
for (element in position.parentsWithSelf) {
(element as? KtExpression)
?.let { this[BindingContext.DATA_FLOW_INFO_BEFORE, it] }
?.let { this[DATA_FLOW_INFO_BEFORE, it] }
?.let { return it }
}
return DataFlowInfo.EMPTY
}
fun KtExpression.isUnreachableCode(context: BindingContext): Boolean = context[BindingContext.UNREACHABLE_CODE, this]!!
fun KtExpression.getReferenceTargets(context: BindingContext): Collection<DeclarationDescriptor> {
val targetDescriptor = if (this is KtReferenceExpression) context[BindingContext.REFERENCE_TARGET, this] else null
return targetDescriptor?.let { listOf(it) } ?: context[BindingContext.AMBIGUOUS_REFERENCE_TARGET, this].orEmpty()
val targetDescriptor = if (this is KtReferenceExpression) context[REFERENCE_TARGET, this] else null
return targetDescriptor?.let { listOf(it) } ?: context[AMBIGUOUS_REFERENCE_TARGET, this].orEmpty()
}
fun KtTypeReference.getAbbreviatedTypeOrType(context: BindingContext) =
context[BindingContext.ABBREVIATED_TYPE, this] ?: context[BindingContext.TYPE, this]
context[ABBREVIATED_TYPE, this] ?: context[TYPE, this]
fun KtTypeElement.getAbbreviatedTypeOrType(context: BindingContext): KotlinType? {
val parent = parent
return when (parent) {
return when (val parent = parent) {
is KtTypeReference -> parent.getAbbreviatedTypeOrType(context)
is KtNullableType -> {
val outerType = parent.getAbbreviatedTypeOrType(context)