KT-11104 : quick fix "surround with null check"

This commit is contained in:
Mikhail Glukhikh
2016-06-01 17:57:45 +03:00
committed by Mikhail Glukhikh
parent 585dcbf1f3
commit 90342ea33b
26 changed files with 353 additions and 0 deletions
@@ -0,0 +1,9 @@
// "Surround with null check" "false"
// ACTION: Introduce local variable
// ACTION: Add non-null asserted (!!) call
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
fun foo(arg: Int?) {
arg?.hashCode()<caret>.toLong()
}
@@ -0,0 +1,8 @@
// "Surround with null check" "false"
// ACTION: Add non-null asserted (!!) call
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
fun foo(arg: Int?) = arg<caret>.hashCode()
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
infix fun Int.op(arg: Int) = this
fun foo(arg: Int?) {
arg <caret>op 42
}
@@ -0,0 +1,9 @@
// "Surround with null check" "true"
infix fun Int.op(arg: Int) = this
fun foo(arg: Int?) {
if (arg != null) {
arg op 42
}
}
@@ -0,0 +1,5 @@
// "Surround with null check" "true"
fun foo(exec: (() -> Unit)?) {
<caret>exec()
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(exec: (() -> Unit)?) {
if (exec != null) {
exec()
}
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
operator fun Int.invoke() = this
fun foo(arg: Int?) {
<caret>arg()
}
@@ -0,0 +1,9 @@
// "Surround with null check" "true"
operator fun Int.invoke() = this
fun foo(arg: Int?) {
if (arg != null) {
arg()
}
}
@@ -0,0 +1,5 @@
// "Surround with null check" "true"
fun foo(arg: Int?) {
arg<caret>.hashCode()
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(arg: Int?) {
if (arg != null) {
arg.hashCode()
}
}
@@ -0,0 +1,5 @@
// "Surround with null check" "true"
fun foo(arg: Int?) {
42 + arg<caret>.hashCode() - 13
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(arg: Int?) {
if (arg != null) {
42 + arg.hashCode() - 13
}
}
@@ -0,0 +1,8 @@
// "Surround with null check" "false"
// ACTION: Add non-null asserted (!!) call
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
fun foo(s: String?) {
val x = s<caret>.hashCode()
}
@@ -0,0 +1,11 @@
// "Surround with null check" "false"
// ACTION: Add non-null asserted (!!) call
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
class My(val x: String?) {
val y: Int
get() = x<caret>.hashCode()
}
@@ -0,0 +1,12 @@
// "Surround with null check" "false"
// WITH_RUNTIME
// ACTION: Add 'block =' to argument
// ACTION: Add non-null asserted (!!) call
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
fun foo(arg: Int?) {
run(fun() = arg<caret>.hashCode())
}
@@ -0,0 +1,5 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
if (flag) arg<caret>.hashCode()
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
if (flag) if (arg != null) {
arg.hashCode()
}
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
when (flag) {
true -> arg<caret>.hashCode()
}
}
@@ -0,0 +1,9 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
when (flag) {
true -> if (arg != null) {
arg.hashCode()
}
}
}
@@ -0,0 +1,5 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
while (flag) arg<caret>.hashCode()
}
@@ -0,0 +1,7 @@
// "Surround with null check" "true"
fun foo(arg: Int?, flag: Boolean) {
while (flag) if (arg != null) {
arg.hashCode()
}
}
@@ -0,0 +1,12 @@
// "Surround with null check" "false"
// ACTION: Introduce local variable
// ACTION: Add non-null asserted (!!) call
// ACTION: Replace with safe (?.) call
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
class My(var x: Int?) {
fun foo() {
x<caret>.hashCode()
}
}