Introduce "use expression body" inspection #KT-16063 Fixed
Converted from the relevant intention Reported cases: one-liners, whens Also, more exact caret detection in local inspection tests
This commit is contained in:
committed by
Mikhail Glukhikh
parent
fe9d3f16d3
commit
d08b18f5f8
+11
@@ -0,0 +1,11 @@
|
||||
interface I {
|
||||
fun foo(): String
|
||||
}
|
||||
|
||||
fun bar(): I {
|
||||
<caret>return object: I {
|
||||
override fun foo(): String {
|
||||
return "a"
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
interface I {
|
||||
fun foo(): String
|
||||
}
|
||||
|
||||
fun bar()<selection>: I</selection> = object: I {
|
||||
override fun foo(): String {
|
||||
return "a"
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
var x = 0
|
||||
|
||||
fun <caret>foo() {
|
||||
x += 1
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// PROBLEM: none
|
||||
|
||||
var a = 1
|
||||
var b = 2
|
||||
|
||||
fun foo() {
|
||||
<caret>a = b
|
||||
}
|
||||
idea/testData/inspectionsLocal/useExpressionBody/convertToExpressionBody/constructorWithEmptyBody.kt
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
class C {
|
||||
constructor()<caret> {}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo() {
|
||||
<caret>val v = 1
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun nullable() {}
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) {
|
||||
<caret>nullable() ?: if (f) bar() else bar()
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun nullable() {}
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) = nullable() ?: if (f) bar() else bar()
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun nullable() {}
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) {
|
||||
<caret>nullable() ?: if (f) bar()
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) {
|
||||
<caret>println("result") ?: when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(): List<String> {
|
||||
ret<caret>urn emptyList()
|
||||
}
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(): List<String> = emptyList()
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(p: Boolean): String {
|
||||
<caret>return bar() ?: return "a"
|
||||
}
|
||||
|
||||
fun bar(): String? = null
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(p: Boolean): String {
|
||||
if (p) {
|
||||
<caret>return "abc"
|
||||
}
|
||||
else {
|
||||
return "def"
|
||||
}
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
fun <caret>foo() {
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
fun foo() = Unit
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
fun <caret>foo(): Unit {
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
fun foo(): Unit = Unit
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo() {
|
||||
<caret>throw UnsupportedOperationException()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo()<selection>: Unit</selection> = throw UnsupportedOperationException()
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo() = <caret>"abc"
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(): Nothing {
|
||||
<caret>throw UnsupportedOperationException()
|
||||
}
|
||||
idea/testData/inspectionsLocal/useExpressionBody/convertToExpressionBody/funWithNothingType.kt.after
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo()<selection>: Nothing</selection> = throw UnsupportedOperationException()
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(): String {
|
||||
<caret>return "abc"
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
fun foo()<selection>: String</selection> = "abc"
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>bar()
|
||||
}
|
||||
|
||||
fun bar() { }
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
fun foo() = bar()
|
||||
|
||||
fun bar() { }
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo() {
|
||||
<caret>bar()
|
||||
}
|
||||
|
||||
fun bar(): String = "abc"
|
||||
idea/testData/inspectionsLocal/useExpressionBody/convertToExpressionBody/funWithUnitTypeWithThrow.kt
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(): Unit {
|
||||
<caret>throw UnsupportedOperationException()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo()<selection>: Unit</selection> = throw UnsupportedOperationException()
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(handler: () -> Unit) { }
|
||||
|
||||
fun bar() {
|
||||
foo { <caret>zoo() }
|
||||
}
|
||||
|
||||
fun zoo(){}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
val foo: String
|
||||
get() {
|
||||
<caret>return "abc"
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
val foo: String
|
||||
get() = "abc"
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) {
|
||||
<caret>if (f) bar() else bar()
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) = if (f) bar() else bar()
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun bar() {}
|
||||
|
||||
fun foo(f: Boolean) {
|
||||
<caret>if (f) bar()
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) {
|
||||
<caret>if (mode) {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
AccessMode.RW -> println("both")
|
||||
}
|
||||
}
|
||||
else {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("noread")
|
||||
AccessMode.WRITE -> println("nowrite")
|
||||
AccessMode.RW -> println("no both")
|
||||
}
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) = if (mode) {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
AccessMode.RW -> println("both")
|
||||
}
|
||||
}
|
||||
else {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("noread")
|
||||
AccessMode.WRITE -> println("nowrite")
|
||||
AccessMode.RW -> println("no both")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// PROBLEM: none
|
||||
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) {
|
||||
<caret>if (mode) {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
}
|
||||
}
|
||||
else {
|
||||
when (access) {
|
||||
AccessMode.READ -> println("noread")
|
||||
AccessMode.WRITE -> println("nowrite")
|
||||
AccessMode.RW -> println("no both")
|
||||
}
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun getText(): String {
|
||||
<caret>return "xxx" //TODO
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
fun getText(): String = "xxx" //TODO
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun getText(): String {
|
||||
// let's return xxx
|
||||
<caret>return "xxx" //TODO
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
fun getText(): String = // let's return xxx
|
||||
"xxx" //TODO
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun getText(): String { // let's return xxx
|
||||
<caret>return "xxx"
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
fun getText(): String = // let's return xxx
|
||||
"xxx"
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun getText(): String {
|
||||
/* let's return xxx */
|
||||
<caret>return "xxx" /* TODO */
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
fun getText(): String = /* let's return xxx */
|
||||
"xxx" /* TODO */
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun <T> run(f: () -> T) = f()
|
||||
fun whenExpr(flag: Boolean) {
|
||||
<caret>run {
|
||||
println("run")
|
||||
if (flag) {
|
||||
println("flag")
|
||||
}
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: true
|
||||
|
||||
fun <T> run(f: () -> T) = f()
|
||||
fun whenExpr(flag: Boolean) = run {
|
||||
println("run")
|
||||
if (flag) {
|
||||
println("flag")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun <T> run(f: () -> T) = f()
|
||||
fun whenExpr(access: AccessMode) {
|
||||
<caret>run {
|
||||
println("run")
|
||||
when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
}
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun <T> run(f: () -> T) = f()
|
||||
fun whenExpr(access: AccessMode) = run {
|
||||
println("run")
|
||||
when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(): String {
|
||||
val v = 1
|
||||
<caret>return "abc"
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
open class A {
|
||||
public open fun foo(): String = ""
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
public override fun foo(): String {
|
||||
<caret>return "abc"
|
||||
}
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
open class A {
|
||||
public open fun foo(): String = ""
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
public override fun foo()<selection>: String</selection> = "abc"
|
||||
}
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
public fun List<String>.fn() : List<String> {
|
||||
<caret>return map {
|
||||
if (it.isEmpty()) return@map "<empty>"
|
||||
it
|
||||
}
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
public fun List<String>.fn() <selection>: List<String></selection><caret> = map {
|
||||
if (it.isEmpty()) return@map "<empty>"
|
||||
it
|
||||
}
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
public fun List<String>.fn() : List<String> {
|
||||
<caret>return map {
|
||||
if (it.isEmpty()) return emptyList()
|
||||
it
|
||||
}
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
public fun List<String>.fn() : List<String> = map {
|
||||
if (it.isEmpty()) return emptyList()
|
||||
it
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo() {
|
||||
<caret>return
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> if (mode) println("read") else println("noread")
|
||||
AccessMode.WRITE -> if (mode) println("write") else println("nowrite")
|
||||
AccessMode.RW -> if (mode) println("both") else println("no both")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: true
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) = when (access) {
|
||||
AccessMode.READ -> if (mode) println("read") else println("noread")
|
||||
AccessMode.WRITE -> if (mode) println("write") else println("nowrite")
|
||||
AccessMode.RW -> if (mode) println("both") else println("no both")
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
// PROBLEM: none
|
||||
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> if (mode) println("read") else println("noread")
|
||||
AccessMode.WRITE -> if (mode) println("write")
|
||||
AccessMode.RW -> if (mode) println("both") else println("no both")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
AccessMode.RW -> println("rw")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
idea/testData/inspectionsLocal/useExpressionBody/convertToExpressionBody/whenUnitExhaustive.kt.after
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) = when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
AccessMode.RW -> println("rw")
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// PROBLEM: none
|
||||
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
else -> println("else")
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(access: AccessMode) = when (access) {
|
||||
AccessMode.READ -> println("read")
|
||||
AccessMode.WRITE -> println("write")
|
||||
else -> println("else")
|
||||
}
|
||||
fun println(s: String) {}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// PROBLEM: none
|
||||
|
||||
enum class AccessMode { READ, WRITE, RW }
|
||||
fun whenExpr(mode: Boolean, access: AccessMode) {
|
||||
<caret>when (access) {
|
||||
AccessMode.READ -> when (mode) {
|
||||
true -> println("read")
|
||||
false -> println("noread")
|
||||
}
|
||||
AccessMode.WRITE -> when (mode) {
|
||||
true -> println("write")
|
||||
}
|
||||
AccessMode.RW -> when (mode) {
|
||||
true -> println("both")
|
||||
else -> println("no both")
|
||||
}
|
||||
}
|
||||
}
|
||||
fun println(s: String) {}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(p: Boolean): String {
|
||||
<caret>while(true) { }
|
||||
}
|
||||
Reference in New Issue
Block a user