Convert "lift return / assignment" intentions into a single inspection

Also includes minor test fix, related to KT-14900
This commit is contained in:
Mikhail Glukhikh
2017-06-27 13:01:33 +03:00
committed by Mikhail Glukhikh
parent 8f33bd0768
commit 2d1abda9a1
91 changed files with 373 additions and 480 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.LiftReturnOrAssignmentInspection
@@ -0,0 +1,18 @@
// WITH_RUNTIME
fun test(x: Any) {
var res: String
<caret>if (x is String)
when {
x.length > 3 -> res = "long string"
else -> res = "short string"
}
else if (x is Int)
when {
x > 999 || x < -99 -> res = "long int"
else -> res = "short int"
}
else if (x is Long)
TODO()
else
res = "I don't know"
}
@@ -0,0 +1,18 @@
// WITH_RUNTIME
fun test(x: Any) {
var res: String
<caret>res = if (x is String)
when {
x.length > 3 -> "long string"
else -> "short string"
}
else if (x is Int)
when {
x > 999 || x < -99 -> "long int"
else -> "short int"
}
else if (x is Long)
TODO()
else
"I don't know"
}
@@ -0,0 +1,9 @@
// PROBLEM: none
fun test(n: Int) {
var a: String = ""
<caret>if (n == 1)
a = "one"
else if (n == 2)
a = "two"
}
@@ -0,0 +1,9 @@
fun test(n: Int) {
val a: String
<caret>if (n == 1)
a = "one"
else if (n == 2)
a = "two"
else
a = "three"
}
@@ -0,0 +1,9 @@
fun test(n: Int) {
val a: String
<caret>a = if (n == 1)
"one"
else if (n == 2)
"two"
else
"three"
}
@@ -0,0 +1,12 @@
// PROBLEM: none
fun test(n: Int) {
val a: String
val b: String
<caret>if (n == 1)
a = "one"
else if (n == 2)
a = "two"
else
b = "three"
}
@@ -0,0 +1,23 @@
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>if (3 > 2) {
doSomething("***")
res = "one"
} else {
doSomething("***")
res = "???"
}
} else if (n == 2) {
doSomething("***")
res = "two"
} else {
doSomething("***")
res = "too many"
}
return res
}
@@ -0,0 +1,23 @@
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String
if (n == 1) {
<caret>res = if (3 > 2) {
doSomething("***")
"one"
} else {
doSomething("***")
"???"
}
} else if (n == 2) {
doSomething("***")
res = "two"
} else {
doSomething("***")
res = "too many"
}
return res
}
@@ -0,0 +1,7 @@
fun test(n: Int): String {
var res: String
<caret>if (n == 1) res = "one" else res = "two"
return res
}
@@ -0,0 +1,7 @@
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) "one" else "two"
return res
}
@@ -0,0 +1,7 @@
fun test(n: Int): String {
var res: String = "!"
<caret>if (n == 1) res += "one" else res += "two"
return res
}
@@ -0,0 +1,7 @@
fun test(n: Int): String {
var res: String = "!"
<caret>res += if (n == 1) "one" else "two"
return res
}
@@ -0,0 +1,15 @@
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
doSomething("***")
res = "one"
} else {
doSomething("***")
res = "two"
}
return res
}
@@ -0,0 +1,15 @@
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String
<caret>res = if (n == 1) {
doSomething("***")
"one"
} else {
doSomething("***")
"two"
}
return res
}
@@ -0,0 +1,18 @@
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String = ""
<caret>if (n == 1) {
doSomething("***")
res = "one"
} else {
var res: String
doSomething("***")
res = "two"
}
return res
}
@@ -0,0 +1,12 @@
// PROBLEM: none
fun test(s: String): Int {
var n: Int = 1;
<caret>if (s.equals("add")) {
n += 1
} else {
n -= 1
}
return n
}
@@ -0,0 +1,13 @@
// PROBLEM: none
fun test(n: Int): String {
var res: String = ""
var res2: String = ""
<caret>if (n == 1) {
res = "one"
} else {
res2 = "two"
}
return res + res2
}
@@ -0,0 +1,13 @@
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String = ""
<caret>if (n == 1) {
doSomething("***")
res = "one"
}
return res
}
@@ -0,0 +1,16 @@
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun test(n: Int): String {
var res: String
<caret>if (n == 1) {
res = "one"
doSomething("***")
} else {
doSomething("***")
res = "two"
}
return res
}