Introduce inspection for safe cast + return #KT-26230 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-08-21 18:32:21 +03:00
committed by Mikhail Glukhikh
parent 2c71b3873e
commit 53c10238d6
20 changed files with 237 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.SafeCastWithReturnInspection
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test(x: Int) {
<caret>x as? String ?: return
}
@@ -0,0 +1,6 @@
fun test(x: Any): Int? {
<caret>(x as? String) ?: (return null)
return foo(x)
}
fun foo(x: String) = 1
@@ -0,0 +1,6 @@
fun test(x: Any): Int? {
if (x !is String) return null
return foo(x)
}
fun foo(x: String) = 1
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Any>) {
list.mapNotNull {
it as? String ?: return@mapNotNull null<caret>
foo(it)
}
}
fun foo(x: String) = 1
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Any>) {
list.mapNotNull {
if (it !is String) return@mapNotNull null
foo(it)
}
}
fun foo(x: String) = 1
@@ -0,0 +1,6 @@
fun test(x: Any): Int? {
<caret>x as? String ?: return null
return foo(x)
}
fun foo(x: String) = 1
@@ -0,0 +1,6 @@
fun test(x: Any): Int? {
if (x !is String) return null
return foo(x)
}
fun foo(x: String) = 1
@@ -0,0 +1,7 @@
// PROBLEM: none
fun test(x: Any): Int? {
val s = <caret>x as? String ?: return null
return foo(s)
}
fun foo(x: String) = 1