Revert "FoldInitializerAndIfToElvisInspection: don't add explicit type if var is used as non-nullable"
This reverts commit bb7d4c22
This commit is contained in:
+10
-14
@@ -84,20 +84,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
|
|||||||
|
|
||||||
val explicitTypeToSet = when {
|
val explicitTypeToSet = when {
|
||||||
// for var with no explicit type, add it so that the actual change won't change
|
// for var with no explicit type, add it so that the actual change won't change
|
||||||
declaration.isVar && declaration.typeReference == null -> {
|
declaration.isVar && declaration.typeReference == null -> initializer.analyze(BodyResolveMode.PARTIAL).getType(initializer)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// for val with explicit type, change it to non-nullable
|
// for val with explicit type, change it to non-nullable
|
||||||
!declaration.isVar && declaration.typeReference != null -> initializer.analyze(BodyResolveMode.PARTIAL).getType(initializer)
|
!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 checkedType = ifExpression.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
|
||||||
val variableType = (prevStatement.resolveToDescriptorIfAny() as? VariableDescriptor)?.type
|
val variableType = (prevStatement.resolveToDescriptorIfAny() as? VariableDescriptor)?.type
|
||||||
if (checkedType != null && variableType != null && !checkedType.isSubtypeOf(variableType)) return null
|
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
|
val statement = if (then is KtBlockExpression) then.statements.singleOrNull() else then
|
||||||
|
|||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// PROBLEM: none
|
||||||
fun test(foo: Int?, bar: Int): Int {
|
fun test(foo: Int?, bar: Int): Int {
|
||||||
var i = foo
|
var i = foo
|
||||||
<caret>if (i == null) {
|
<caret>if (i == null) {
|
||||||
|
|||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
fun test(foo: Int?, bar: Int): Int {
|
|
||||||
var i = foo ?: return bar
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
+1
@@ -1,3 +1,4 @@
|
|||||||
|
// PROBLEM: none
|
||||||
fun test(foo: Int?, bar: Int): Int {
|
fun test(foo: Int?, bar: Int): Int {
|
||||||
var i = foo
|
var i = foo
|
||||||
<caret>if (i == null) {
|
<caret>if (i == null) {
|
||||||
|
|||||||
-8
@@ -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?) {}
|
|
||||||
Reference in New Issue
Block a user