Add IDEA data-flow analysis to guess nullability

Add "if return..." folding to "return if"
This commit is contained in:
Simon Ogorodnik
2017-05-25 15:54:10 +03:00
parent 1f26353de4
commit e41c027c9a
24 changed files with 261 additions and 40 deletions
@@ -3,7 +3,7 @@
internal class Library {
fun call() {}
val string: String?
val string: String
get() = ""
}
@@ -11,9 +11,9 @@ internal class User {
fun main() {
val lib: Library = Library()
lib.call()
lib.string!!.isEmpty()
lib.string.isEmpty()
Library().call()
Library().string!!.isEmpty()
Library().string.isEmpty()
}
}
@@ -1,7 +1,6 @@
// ERROR: Type mismatch: inferred type is String? but Any was expected
internal class A {
private val s: String? = null
val value: Any
val value: Any?
get() = s
}
+1 -1
View File
@@ -1 +1 @@
if (true) return 1 else return 0
return if (true) 1 else 0
+3 -3
View File
@@ -11,9 +11,9 @@ object Test {
return false
}
val result = true
if (parent.isDirectory) {
return true
return if (parent.isDirectory) {
true
} else
return false
false
}
}
+1 -3
View File
@@ -8,9 +8,7 @@ internal class Test {
if (s1.isEmpty() && s2.isEmpty())
return "OK"
if (s1.isEmpty() && s2.isEmpty() && s3.isEmpty())
return "OOOK"
return if (s1.isEmpty() && s2.isEmpty() && s3.isEmpty()) "OOOK" else ""
return ""
}
}
@@ -1,9 +1,9 @@
internal class C {
fun foo(b: Boolean): String? {
if (b) {
return "abc"
return if (b) {
"abc"
} else {
return null
null
}
}
}
@@ -1,5 +1,4 @@
fun foo(s: String?, b: Boolean): Int {
if (s == null) println("null")
if (b) return s!!.length
return 10
return if (b) s!!.length else 10
}
+1 -1
View File
@@ -25,7 +25,7 @@ internal class C : Base(), I {
return ""
}
override fun zoo(o: Any?): String? {
override fun zoo(o: Any?): String {
return ""
}
}
+1 -4
View File
@@ -1,8 +1,5 @@
internal class A {
fun foo(s: String?): Int {
if (s != null) {
return s.length
}
return -1
return s?.length ?: -1
}
}
+1 -2
View File
@@ -1,4 +1,3 @@
// ERROR: Type mismatch: inferred type is String? but String was expected
// ERROR: Type mismatch: inferred type is Int? but Int was expected
// ERROR: Type inference failed. Please try to specify type arguments explicitly.
// ERROR: Using 'remove(Int): T' is an error. Use removeAt(index) instead.
@@ -50,7 +49,7 @@ class Test {
}
fun foo2(s: String) {
fun foo2(s: String?) {
}
@@ -1,20 +1,18 @@
// ERROR: Type mismatch: inferred type is String? but String was expected
// ERROR: Type mismatch: inferred type is String? but String was expected
class Test {
fun nullableString(p: Int): String? {
return if (p > 0) "response" else null
}
private var nullableInitializerField: String = nullableString(3)
private var nullableInitializerField = nullableString(3)
private val nullableInitializerFieldFinal = nullableString(3)
var nullableInitializerPublicField: String = nullableString(3)
var nullableInitializerPublicField = nullableString(3)
fun testProperty() {
nullableInitializerField = "aaa"
nullableInitializerField[0]
nullableInitializerField!![0]
nullableInitializerFieldFinal!![0]
nullableInitializerPublicField[0]
nullableInitializerPublicField!![0]
}
fun testLocalVariable() {
+3 -3
View File
@@ -1,9 +1,9 @@
fun foo(i: Int, j: Int): String {
when (i) {
0 -> if (j > 0) {
return "1"
0 -> return if (j > 0) {
"1"
} else {
return "2"
"2"
}
1 -> return "3"
else -> return "4"
+3 -4
View File
@@ -1,10 +1,9 @@
fun foo(i: Int, j: Int): String {
when (i) {
0 -> {
if (j > 0) {
return "1"
}
return "2"
return if (j > 0) {
"1"
} else "2"
}
1 -> return "2"
else -> return "3"
@@ -1,7 +1,6 @@
internal class A {
private fun foo(o: Any?, b: Boolean): String? {
if (b) return o as String?
return ""
return if (b) o as String? else ""
}
fun bar() {