Fix all illegal usages of safeAs function

Call of `safeAs` without specifying explicit type argument is hardly
  readable and may leads to hardly detectable errors
This commit is contained in:
Dmitriy Novozhilov
2021-07-09 14:21:47 +03:00
parent 5072653957
commit ddc3ef1121
9 changed files with 16 additions and 14 deletions
@@ -127,7 +127,7 @@ fun FirTypeRef.toRegularClass(session: FirSession): FirRegularClass? {
inline fun <reified T : Any> FirQualifiedAccessExpression.getDeclaration(): T? { inline fun <reified T : Any> FirQualifiedAccessExpression.getDeclaration(): T? {
return this.calleeReference.safeAs<FirResolvedNamedReference>() return this.calleeReference.safeAs<FirResolvedNamedReference>()
?.resolvedSymbol ?.resolvedSymbol
?.fir.safeAs() ?.fir as? T
} }
/** /**
@@ -94,5 +94,5 @@ object FirCommonConstructorDelegationIssuesChecker : FirRegularClassChecker() {
private fun FirConstructor.getDelegated(): FirConstructor? = delegatedConstructor private fun FirConstructor.getDelegated(): FirConstructor? = delegatedConstructor
?.calleeReference.safeAs<FirResolvedNamedReference>() ?.calleeReference.safeAs<FirResolvedNamedReference>()
?.resolvedSymbol ?.resolvedSymbol
?.fir.safeAs() ?.fir as? FirConstructor?
} }
@@ -108,7 +108,7 @@ object FirOverrideChecker : FirClassChecker() {
overriddenSymbols: List<FirCallableSymbol<*>>, overriddenSymbols: List<FirCallableSymbol<*>>,
): FirMemberDeclaration? { ): FirMemberDeclaration? {
if (isVar) return null if (isVar) return null
return overriddenSymbols.find { (it.fir as? FirProperty)?.isVar == true }?.fir?.safeAs() return overriddenSymbols.find { (it.fir as? FirProperty)?.isVar == true }?.fir as? FirMemberDeclaration?
} }
private fun FirCallableMemberDeclaration.checkVisibility( private fun FirCallableMemberDeclaration.checkVisibility(
@@ -178,7 +178,7 @@ object FirOverrideChecker : FirClassChecker() {
AbstractTypeChecker.isSubtypeOf(typeCheckerContext, overridingReturnType, overriddenReturnType) AbstractTypeChecker.isSubtypeOf(typeCheckerContext, overridingReturnType, overriddenReturnType)
if (!isReturnTypeOkForOverride) { if (!isReturnTypeOkForOverride) {
return overriddenDeclaration.safeAs() return overriddenDeclaration as? FirMemberDeclaration
} }
} }
@@ -29,7 +29,7 @@ object FirUpperBoundViolatedExpressionChecker : FirQualifiedAccessExpressionChec
val calleReference = expression.calleeReference val calleReference = expression.calleeReference
var calleeFir: FirTypeParameterRefsOwner? = null var calleeFir: FirTypeParameterRefsOwner? = null
if (calleReference is FirResolvedNamedReference) { if (calleReference is FirResolvedNamedReference) {
calleeFir = calleReference.safeAs<FirResolvedNamedReference>()?.resolvedSymbol?.fir.safeAs() calleeFir = calleReference.safeAs<FirResolvedNamedReference>()?.resolvedSymbol?.fir as? FirTypeParameterRefsOwner
} else if (calleReference is FirErrorNamedReference) { } else if (calleReference is FirErrorNamedReference) {
val diagnostic = calleReference.diagnostic val diagnostic = calleReference.diagnostic
if (diagnostic is ConeInapplicableCandidateError && if (diagnostic is ConeInapplicableCandidateError &&
@@ -37,7 +37,7 @@ object FirUpperBoundViolatedExpressionChecker : FirQualifiedAccessExpressionChec
) { ) {
return return
} }
calleeFir = calleReference.candidateSymbol?.fir.safeAs() calleeFir = calleReference.candidateSymbol?.fir as? FirTypeParameterRefsOwner
} }
var typeArguments: List<Any>? = null var typeArguments: List<Any>? = null
@@ -61,4 +61,4 @@ object FirUpperBoundViolatedExpressionChecker : FirQualifiedAccessExpressionChec
typeArgumentRefsAndSources typeArgumentRefsAndSources
) )
} }
} }
@@ -66,7 +66,7 @@ private fun ConeDiagnostic.toFirDiagnostic(
is FirBackingFieldSymbol -> FirErrors.VAL_REASSIGNMENT_VIA_BACKING_FIELD_ERROR.createOn(source, symbol.fir.symbol) is FirBackingFieldSymbol -> FirErrors.VAL_REASSIGNMENT_VIA_BACKING_FIELD_ERROR.createOn(source, symbol.fir.symbol)
else -> FirErrors.VAL_REASSIGNMENT.createOn(source, symbol) else -> FirErrors.VAL_REASSIGNMENT.createOn(source, symbol)
} }
is ConeUnexpectedTypeArgumentsError -> FirErrors.TYPE_ARGUMENTS_NOT_ALLOWED.createOn(this.source.safeAs() ?: source) is ConeUnexpectedTypeArgumentsError -> FirErrors.TYPE_ARGUMENTS_NOT_ALLOWED.createOn(this.source as? FirSourceElement ?: source)
is ConeIllegalAnnotationError -> FirErrors.NOT_AN_ANNOTATION_CLASS.createOn(source, this.name.asString()) is ConeIllegalAnnotationError -> FirErrors.NOT_AN_ANNOTATION_CLASS.createOn(source, this.name.asString())
is ConeWrongNumberOfTypeArgumentsError -> is ConeWrongNumberOfTypeArgumentsError ->
FirErrors.WRONG_NUMBER_OF_TYPE_ARGUMENTS.createOn(qualifiedAccessSource ?: source, this.desiredCount, this.type) FirErrors.WRONG_NUMBER_OF_TYPE_ARGUMENTS.createOn(qualifiedAccessSource ?: source, this.desiredCount, this.type)
@@ -88,7 +88,7 @@ private fun <T : FirAnnotatedDeclaration> T.getDeprecationForCallSite(
private fun FirAnnotationCall.getStringArgument(name: Name): String? = private fun FirAnnotationCall.getStringArgument(name: Name): String? =
findArgumentByName(name)?.let { expression -> findArgumentByName(name)?.let { expression ->
expression.safeAs<FirConstExpression<*>>()?.value.safeAs() expression.safeAs<FirConstExpression<*>>()?.value as? String
} }
private fun FirAnnotationCall.getVersionFromArgument(name: Name): ApiVersion? = private fun FirAnnotationCall.getVersionFromArgument(name: Name): ApiVersion? =
@@ -435,7 +435,9 @@ class DiagnosticReporterByTrackingStrategy(
error as CapturedTypeFromSubtyping error as CapturedTypeFromSubtyping
val position = error.position val position = error.position
val argumentPosition: ArgumentConstraintPositionImpl? = val argumentPosition: ArgumentConstraintPositionImpl? =
position.safeAs() ?: position.safeAs<IncorporationConstraintPosition>()?.from.safeAs() position.safeAs<ArgumentConstraintPositionImpl>()
?: position.safeAs<IncorporationConstraintPosition>()
?.from.safeAs<ArgumentConstraintPositionImpl>()
argumentPosition?.let { argumentPosition?.let {
val expression = it.argument.psiExpression ?: return val expression = it.argument.psiExpression ?: return
@@ -281,7 +281,7 @@ class KotlinResolutionCallbacksImpl(
private fun findCommonParent(callElement: KtExpression, receiver: ReceiverKotlinCallArgument?): KtExpression { private fun findCommonParent(callElement: KtExpression, receiver: ReceiverKotlinCallArgument?): KtExpression {
if (receiver == null) return callElement if (receiver == null) return callElement
return PsiTreeUtil.findCommonParent(callElement, receiver.psiExpression)?.safeAs() ?: callElement return PsiTreeUtil.findCommonParent(callElement, receiver.psiExpression) as? KtExpression? ?: callElement
} }
override fun getExpectedTypeFromAsExpressionAndRecordItInTrace(resolvedAtom: ResolvedCallAtom): UnwrappedType? { override fun getExpectedTypeFromAsExpressionAndRecordItInTrace(resolvedAtom: ResolvedCallAtom): UnwrappedType? {
@@ -684,8 +684,8 @@ fun KtExpression.getBinaryWithTypeParent(): KtBinaryExpressionWithTypeRHS? {
fun KtExpression.topParenthesizedParentOrMe(): KtExpression { fun KtExpression.topParenthesizedParentOrMe(): KtExpression {
var result: KtExpression = this var result: KtExpression = this
while (KtPsiUtil.deparenthesizeOnce(result.parent.safeAs()) == result) { while (KtPsiUtil.deparenthesizeOnce(result.parent as? KtExpression) == result) {
result = result.parent.safeAs() ?: break result = result.parent as? KtExpression ?: break
} }
return result return result
} }
@@ -711,4 +711,4 @@ tailrec fun KtTypeElement.unwrapNullability(): KtTypeElement? {
is KtDefinitelyNotNullType -> this.innerType?.unwrapNullability() is KtDefinitelyNotNullType -> this.innerType?.unwrapNullability()
else -> this else -> this
} }
} }