Add inspection to replace not-null assertion with elvis return

#KT-30381 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-03-12 12:05:00 +09:00
committed by Dmitry Gridin
parent 1c0c01725b
commit dffcfdc02f
24 changed files with 282 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ReplaceNotNullAssertionWithElvisReturnInspection
@@ -0,0 +1,3 @@
fun test(i: Int?) {
val x = i!!<caret>
}
@@ -0,0 +1,3 @@
fun test(i: Int?) {
val x = i ?: return
}
@@ -0,0 +1,5 @@
fun foo(): Int? = null
fun test() {
foo()!!<caret>
}
@@ -0,0 +1,5 @@
fun foo(): Int? = null
fun test() {
foo() ?: return
}
@@ -0,0 +1,6 @@
class Foo(val i: Int?)
fun test(foo: Foo): Int? {
val x = foo.i!!<caret>
return x
}
@@ -0,0 +1,6 @@
class Foo(val i: Int?)
fun test(foo: Foo): Int? {
val x = foo.i ?: return null
return x
}
@@ -0,0 +1,8 @@
class Foo {
fun baz(): Int? = null
}
fun test(foo: Foo): Int? {
foo.baz()!!<caret>
return 0
}
@@ -0,0 +1,8 @@
class Foo {
fun baz(): Int? = null
}
fun test(foo: Foo): Int? {
foo.baz() ?: return null
return 0
}
@@ -0,0 +1,3 @@
fun test(i: Int?) {
val x = i!!<caret> + 1
}
@@ -0,0 +1,3 @@
fun test(i: Int?) {
val x = (i ?: return) + 1
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(list: List<String>, number: Int?) {
list.forEach {
number!!<caret>
}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(list: List<String>, number: Int?) {
list.forEach {
number ?: return@forEach
}
}
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<String>, number: Int?) {
val x: List<Int> = list.map {
number!!<caret>
}
}
@@ -0,0 +1,5 @@
// PROBLEM: none
fun test(i: Int?): Int {
val x = i!!<caret>
return x
}
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test(i: Int?): Int? {
return i!!<caret>
}
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test(i: Int?): Int? {
return (i!!<caret>)
}
@@ -0,0 +1,5 @@
fun test(a: Int?): Int? {
return check(a!!<caret>)
}
fun check(i: Int): Int = i
@@ -0,0 +1,5 @@
fun test(a: Int?): Int? {
return check(a ?: return null)
}
fun check(i: Int): Int = i
@@ -0,0 +1,4 @@
// PROBLEM: none
fun test(i: Int) {
val x = i!!<caret>
}