Minor. Reformat JavaNullabilityChecker
This commit is contained in:
+38
-31
@@ -41,10 +41,10 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
|
|
||||||
override fun checkType(expression: KtExpression, expressionType: KotlinType, expressionTypeWithSmartCast: KotlinType, c: ResolutionContext<*>) {
|
override fun checkType(expression: KtExpression, expressionType: KotlinType, expressionTypeWithSmartCast: KotlinType, c: ResolutionContext<*>) {
|
||||||
doCheckType(
|
doCheckType(
|
||||||
expressionType,
|
expressionType,
|
||||||
c.expectedType,
|
c.expectedType,
|
||||||
{ c.dataFlowValueFactory.createDataFlowValue(expression, expressionType, c) } ,
|
{ c.dataFlowValueFactory.createDataFlowValue(expression, expressionType, c) },
|
||||||
c.dataFlowInfo
|
c.dataFlowInfo
|
||||||
) { expectedType, actualType ->
|
) { expectedType, actualType ->
|
||||||
c.trace.report(ErrorsJvm.NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS.on(expression, expectedType, actualType))
|
c.trace.report(ErrorsJvm.NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS.on(expression, expectedType, actualType))
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,8 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
val enumClassDescriptor = WhenChecker.getClassDescriptorOfTypeIfEnum(type) ?: return
|
val enumClassDescriptor = WhenChecker.getClassDescriptorOfTypeIfEnum(type) ?: return
|
||||||
val context = c.trace.bindingContext
|
val context = c.trace.bindingContext
|
||||||
if (WhenChecker.getEnumMissingCases(expression, context, enumClassDescriptor).isEmpty()
|
if (WhenChecker.getEnumMissingCases(expression, context, enumClassDescriptor).isEmpty()
|
||||||
&& !WhenChecker.containsNullCase(expression, context)) {
|
&& !WhenChecker.containsNullCase(expression, context)
|
||||||
|
) {
|
||||||
val subjectDataFlowValue = c.dataFlowValueFactory.createDataFlowValue(subjectExpression, type, c)
|
val subjectDataFlowValue = c.dataFlowValueFactory.createDataFlowValue(subjectExpression, type, c)
|
||||||
val dataFlowInfo = c.trace[BindingContext.EXPRESSION_TYPE_INFO, subjectExpression]?.dataFlowInfo
|
val dataFlowInfo = c.trace[BindingContext.EXPRESSION_TYPE_INFO, subjectExpression]?.dataFlowInfo
|
||||||
if (dataFlowInfo != null && !dataFlowInfo.getStableNullability(subjectDataFlowValue).canBeNull()) {
|
if (dataFlowInfo != null && !dataFlowInfo.getStableNullability(subjectDataFlowValue).canBeNull()) {
|
||||||
@@ -75,9 +76,9 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
val baseExpression = expression.baseExpression ?: return
|
val baseExpression = expression.baseExpression ?: return
|
||||||
val baseExpressionType = c.trace.getType(baseExpression) ?: return
|
val baseExpressionType = c.trace.getType(baseExpression) ?: return
|
||||||
doIfNotNull(
|
doIfNotNull(
|
||||||
baseExpressionType,
|
baseExpressionType,
|
||||||
{ c.dataFlowValueFactory.createDataFlowValue(baseExpression, baseExpressionType, c) },
|
{ c.dataFlowValueFactory.createDataFlowValue(baseExpression, baseExpressionType, c) },
|
||||||
c
|
c
|
||||||
) {
|
) {
|
||||||
c.trace.report(Errors.UNNECESSARY_NOT_NULL_ASSERTION.on(expression.operationReference, baseExpressionType))
|
c.trace.report(Errors.UNNECESSARY_NOT_NULL_ASSERTION.on(expression.operationReference, baseExpressionType))
|
||||||
}
|
}
|
||||||
@@ -90,11 +91,11 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
KtTokens.EXCLEQEQEQ -> {
|
KtTokens.EXCLEQEQEQ -> {
|
||||||
if (expression.left != null && expression.right != null) {
|
if (expression.left != null && expression.right != null) {
|
||||||
SenselessComparisonChecker.checkSenselessComparisonWithNull(
|
SenselessComparisonChecker.checkSenselessComparisonWithNull(
|
||||||
expression, expression.left!!, expression.right!!, c,
|
expression, expression.left!!, expression.right!!, c,
|
||||||
{ c.trace.getType(it) },
|
{ c.trace.getType(it) },
|
||||||
{ value ->
|
{ value ->
|
||||||
doIfNotNull(value.type, { value }, c) { Nullability.NOT_NULL } ?: Nullability.UNKNOWN
|
doIfNotNull(value.type, { value }, c) { Nullability.NOT_NULL } ?: Nullability.UNKNOWN
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,7 +103,12 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun checkReceiver(receiverParameter: ReceiverParameterDescriptor, receiverArgument: ReceiverValue, safeAccess: Boolean, c: CallResolutionContext<*>) {
|
override fun checkReceiver(
|
||||||
|
receiverParameter: ReceiverParameterDescriptor,
|
||||||
|
receiverArgument: ReceiverValue,
|
||||||
|
safeAccess: Boolean,
|
||||||
|
c: CallResolutionContext<*>
|
||||||
|
) {
|
||||||
val dataFlowValue by lazy(LazyThreadSafetyMode.NONE) {
|
val dataFlowValue by lazy(LazyThreadSafetyMode.NONE) {
|
||||||
c.dataFlowValueFactory.createDataFlowValue(receiverArgument, c)
|
c.dataFlowValueFactory.createDataFlowValue(receiverArgument, c)
|
||||||
}
|
}
|
||||||
@@ -117,10 +123,10 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doCheckType(
|
doCheckType(
|
||||||
receiverArgument.type,
|
receiverArgument.type,
|
||||||
receiverParameter.type,
|
receiverParameter.type,
|
||||||
{ dataFlowValue },
|
{ dataFlowValue },
|
||||||
c.dataFlowInfo
|
c.dataFlowInfo
|
||||||
) { expectedType,
|
) { expectedType,
|
||||||
actualType ->
|
actualType ->
|
||||||
val receiverExpression = (receiverArgument as? ExpressionReceiver)?.expression
|
val receiverExpression = (receiverArgument as? ExpressionReceiver)?.expression
|
||||||
@@ -134,11 +140,11 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun doCheckType(
|
private fun doCheckType(
|
||||||
expressionType: KotlinType,
|
expressionType: KotlinType,
|
||||||
expectedType: KotlinType,
|
expectedType: KotlinType,
|
||||||
dataFlowValue: () -> DataFlowValue,
|
dataFlowValue: () -> DataFlowValue,
|
||||||
dataFlowInfo: DataFlowInfo,
|
dataFlowInfo: DataFlowInfo,
|
||||||
reportWarning: (expectedType: KotlinType, actualType: KotlinType) -> Unit
|
reportWarning: (expectedType: KotlinType, actualType: KotlinType) -> Unit
|
||||||
) {
|
) {
|
||||||
if (TypeUtils.noExpectedType(expectedType)) {
|
if (TypeUtils.noExpectedType(expectedType)) {
|
||||||
return
|
return
|
||||||
@@ -157,15 +163,16 @@ class JavaNullabilityChecker : AdditionalTypeChecker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Any> doIfNotNull(
|
private fun <T : Any> doIfNotNull(
|
||||||
type: KotlinType,
|
type: KotlinType,
|
||||||
dataFlowValue: () -> DataFlowValue,
|
dataFlowValue: () -> DataFlowValue,
|
||||||
c: ResolutionContext<*>,
|
c: ResolutionContext<*>,
|
||||||
body: () -> T
|
body: () -> T
|
||||||
) = if (type.mustNotBeNull()?.isFromJava == true &&
|
) = if (type.mustNotBeNull()?.isFromJava == true &&
|
||||||
c.dataFlowInfo.getStableNullability(dataFlowValue()).canBeNull())
|
c.dataFlowInfo.getStableNullability(dataFlowValue()).canBeNull()
|
||||||
body()
|
)
|
||||||
else
|
body()
|
||||||
null
|
else
|
||||||
|
null
|
||||||
|
|
||||||
private fun KotlinType.mayBeNull(): EnhancedNullabilityInfo? = when {
|
private fun KotlinType.mayBeNull(): EnhancedNullabilityInfo? = when {
|
||||||
!isError && !isFlexible() && TypeUtils.acceptsNullable(this) -> enhancementFromKotlin()
|
!isError && !isFlexible() && TypeUtils.acceptsNullable(this) -> enhancementFromKotlin()
|
||||||
|
|||||||
Reference in New Issue
Block a user