[FE] Add clear warning about future changes about nullability of safe call with non nullable receiver

^KT-46860
This commit is contained in:
Dmitriy Novozhilov
2021-10-26 17:35:31 +03:00
committed by teamcityserver
parent 937f4c1dab
commit f26059a7d3
122 changed files with 323 additions and 283 deletions
@@ -1007,6 +1007,7 @@ public interface Errors {
DiagnosticFactory3<KtExpression, PsiElement, String, PsiElement> UNSAFE_INFIX_CALL = DiagnosticFactory3.create(ERROR);
DiagnosticFactory3<KtExpression, PsiElement, String, PsiElement> UNSAFE_OPERATOR_CALL = DiagnosticFactory3.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> UNNECESSARY_SAFE_CALL = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<PsiElement> SAFE_CALL_WILL_CHANGE_NULLABILITY = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> UNEXPECTED_SAFE_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> UNNECESSARY_NOT_NULL_ASSERTION = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<PsiElement> NOT_NULL_ASSERTION_ON_LAMBDA_EXPRESSION = DiagnosticFactory0.create(WARNING);
@@ -718,6 +718,7 @@ public class DefaultErrorMessages {
MAP.put(EXCEPTION_FROM_ANALYZER, "Internal Error occurred while analyzing this expression:\n{0}", THROWABLE);
MAP.put(MISSING_STDLIB, "{0}. Ensure you have the standard Kotlin library in dependencies", STRING);
MAP.put(UNNECESSARY_SAFE_CALL, "Unnecessary safe call on a non-null receiver of type {0}. This expression will have nullable type in future releases", RENDER_TYPE);
MAP.put(SAFE_CALL_WILL_CHANGE_NULLABILITY, "Safe call on a non-null receiver will have nullable type in future releases");
MAP.put(UNEXPECTED_SAFE_CALL, "Safe-call is not allowed here");
MAP.put(UNNECESSARY_NOT_NULL_ASSERTION, "Unnecessary non-null assertion (!!) on a non-null receiver of type {0}", RENDER_TYPE);
MAP.put(NOT_NULL_ASSERTION_ON_LAMBDA_EXPRESSION, "Non-null assertion (!!) is called on a lambda expression");
@@ -366,7 +366,14 @@ class CallExpressionResolver(
)
}
if (!receiverCanBeNull) {
reportUnnecessarySafeCall(context.trace, receiver.type, callOperationNode, receiver)
reportUnnecessarySafeCall(
context.trace,
receiver.type,
element.qualified,
callOperationNode,
receiver,
context.languageVersionSettings
)
}
}
@@ -530,13 +537,18 @@ class CallExpressionResolver(
fun reportUnnecessarySafeCall(
trace: BindingTrace,
type: KotlinType,
callElement: KtQualifiedExpression,
callOperationNode: ASTNode,
explicitReceiver: Receiver?
explicitReceiver: Receiver?,
languageVersionSettings: LanguageVersionSettings
) {
if (explicitReceiver is ExpressionReceiver && explicitReceiver.expression is KtSuperExpression) {
trace.report(UNEXPECTED_SAFE_CALL.on(callOperationNode.psi))
} else if (!type.isError) {
trace.report(UNNECESSARY_SAFE_CALL.on(callOperationNode.psi, type))
if (!languageVersionSettings.supportsFeature(LanguageFeature.SafeCallsAreAlwaysNullable)) {
trace.report(SAFE_CALL_WILL_CHANGE_NULLABILITY.on(callElement))
}
}
}