FIR IDE: Offer AddExclExclCallFix for nullable types with an

`iterator()` function that does NOT have `operator` modifier.

This is different from FE1.0. Adding `!!` will then surface the error
that `operator` modifier needs to be added (with corresponding fix).
This commit is contained in:
Mark Punzalan
2021-05-01 05:33:45 +00:00
committed by Ilya Kirillov
parent 9e01a608b2
commit 1d9247ae0f
3 changed files with 24 additions and 2 deletions
@@ -81,9 +81,11 @@ object AddExclExclCallFixFactories {
val type = expression.getKtType()
if (!type.canBeNull) return@diagnosticFixFactory emptyList()
// NOTE: This is different from FE1.0 in that we offer the fix even if the function does NOT have the `operator` modifier.
// Adding `!!` will then surface the error that `operator` should be added (with corresponding fix).
val typeScope = type.getTypeScope() ?: return@diagnosticFixFactory emptyList()
val hasValidIterator = typeScope.getCallableSymbols { it == OperatorNameConventions.ITERATOR }
.filter { it is KtFunctionSymbol && it.isOperator && it.valueParameters.isEmpty() }.singleOrNull() != null
.filter { it is KtFunctionSymbol && it.valueParameters.isEmpty() }.singleOrNull() != null
if (hasValidIterator) {
listOfNotNull(expression.asAddExclExclCallFix())
} else {
@@ -93,4 +95,4 @@ object AddExclExclCallFixFactories {
}
internal fun PsiElement?.asAddExclExclCallFix(hasImplicitReceiver: Boolean = false) =
this?.let { AddExclExclCallFix(it, hasImplicitReceiver) } ?: null
this?.let { AddExclExclCallFix(it, hasImplicitReceiver) }
@@ -0,0 +1,10 @@
// "Add non-null asserted (!!) call" "true"
class Some {
fun iterator(): Iterator<Int> = null!!
}
fun foo() {
val test: Some? = Some()
for (i in <caret>test) { }
}
@@ -0,0 +1,10 @@
// "Add non-null asserted (!!) call" "true"
class Some {
fun iterator(): Iterator<Int> = null!!
}
fun foo() {
val test: Some? = Some()
for (i in test!!) { }
}