Revert "FoldInitializerAndIfToElvisInspection: don't add explicit type if var is used as non-nullable"

This reverts commit bb7d4c22
This commit is contained in:
Yan Zhulanow
2020-10-24 00:22:18 +09:00
parent 87574dddd9
commit c07f25fa44
5 changed files with 12 additions and 26 deletions
@@ -84,20 +84,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
val explicitTypeToSet = when {
// for var with no explicit type, add it so that the actual change won't change
declaration.isVar && declaration.typeReference == null -> {
if (element.condition is KtBinaryExpression) {
val ifEndOffset = element.endOffset
val context = element.analyze()
val isUsedAsNotNullable = ReferencesSearch.search(declaration, LocalSearchScope(declaration.parent)).any {
if (it.element.startOffset <= ifEndOffset) return@any false
val type = it.element.safeAs<KtExpression>()?.getType(context) ?: return@any false
!type.isNullable()
}
if (isUsedAsNotNullable) null else context.getType(initializer)
} else {
initializer.analyze(BodyResolveMode.PARTIAL).getType(initializer)
}
}
declaration.isVar && declaration.typeReference == null -> initializer.analyze(BodyResolveMode.PARTIAL).getType(initializer)
// for val with explicit type, change it to non-nullable
!declaration.isVar && declaration.typeReference != null -> initializer.analyze(BodyResolveMode.PARTIAL).getType(initializer)
@@ -166,6 +153,15 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
val checkedType = ifExpression.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
val variableType = (prevStatement.resolveToDescriptorIfAny() as? VariableDescriptor)?.type
if (checkedType != null && variableType != null && !checkedType.isSubtypeOf(variableType)) return null
} else if (prevStatement.isVar && operationExpression is KtBinaryExpression) {
val ifEndOffset = ifExpression.endOffset
val context = ifExpression.analyze()
val isUsedAsNotNullable = ReferencesSearch.search(prevStatement, LocalSearchScope(prevStatement.parent)).any {
if (it.element.startOffset <= ifEndOffset) return@any false
val type = it.element.safeAs<KtExpression>()?.getType(context) ?: return@any false
!type.isNullable()
}
if (isUsedAsNotNullable) return null
}
val statement = if (then is KtBlockExpression) then.statements.singleOrNull() else then
@@ -1,3 +1,4 @@
// PROBLEM: none
fun test(foo: Int?, bar: Int): Int {
var i = foo
<caret>if (i == null) {
@@ -1,4 +0,0 @@
fun test(foo: Int?, bar: Int): Int {
var i = foo ?: return bar
return i
}
@@ -1,3 +1,4 @@
// PROBLEM: none
fun test(foo: Int?, bar: Int): Int {
var i = foo
<caret>if (i == null) {
@@ -1,8 +0,0 @@
fun test(foo: Int?, bar: Int): Int {
var i = foo ?: return bar
baz(i)
val j = i + 1
return j
}
fun baz(i: Int?) {}