Convert lambda to callable reference intention / inspection #KT-10903 Fixed
(cherry picked from commit b620099)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b5bdb070a0
commit
220141ab2b
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ConvertLambdaToReferenceIntention
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class Person(val name: String)
|
||||
|
||||
val x = listOf("Jack", "Tom").map <caret>{ Person(it) }
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class Person(val name: String)
|
||||
|
||||
val x = listOf("Jack", "Tom").map(::Person)
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val x = listOf("123", "4567").map <caret>{ it.toInt() }
|
||||
@@ -0,0 +1,3 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
val x = listOf("123", "4567").map(String::toInt)
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo(z: Int, y: Int = 0) = y + z
|
||||
|
||||
val x = { arg: Int <caret>-> foo(arg) }
|
||||
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun List<Int>.transform(x: Int = 0, f: (Int) -> Int) = map { f(it + x) }
|
||||
|
||||
fun bar(x: Int) = x * x
|
||||
|
||||
val y = listOf(1, 2, 3).transform { <caret>bar(it) }
|
||||
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
interface Transformer {
|
||||
fun transform(x: Int = 0, f: (Int) -> Int) = f(x)
|
||||
}
|
||||
|
||||
class TransformerImpl : Transformer
|
||||
|
||||
fun bar(x: Int) = x * x
|
||||
|
||||
val y = TransformerImpl().transform { <caret>bar(it) }
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar(s: String) = s.length
|
||||
|
||||
val x = listOf("Jack", "Tom").map() <caret>{ w -> bar(w) }
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar(s: String) = s.length
|
||||
|
||||
val x = listOf("Jack", "Tom").map(::bar)
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Generic<T : Any> {
|
||||
val y = { arg: T <caret>-> arg.hashCode() }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Owner(val z: Int) {
|
||||
// Possible only in 1.1 with bound references (this::foo)
|
||||
val x = { arg: Int <caret> -> foo(arg) }
|
||||
}
|
||||
|
||||
fun Owner.foo(y: Int) = y + z
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
val Any.name: String get() = toString()
|
||||
|
||||
val converted = { x: Any -> <caret>x.name }
|
||||
@@ -0,0 +1,3 @@
|
||||
val Any.name: String get() = toString()
|
||||
|
||||
val converted = Any::name
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun <T> id(y: T) = y
|
||||
|
||||
val x = { arg: Int <caret>-> id<Int>(arg) }
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
val name = "Kotlin"
|
||||
val x = { obj: Any -> <caret>name }
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
val name = "Kotlin"
|
||||
val x = { -> <caret>name }
|
||||
@@ -0,0 +1,5 @@
|
||||
class Owner {
|
||||
inner class Inner
|
||||
|
||||
val x = { <caret>o: Owner -> o.Inner() }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Owner {
|
||||
inner class Inner
|
||||
|
||||
val x = Owner::Inner
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar(s: String) = s.length
|
||||
|
||||
val x = listOf("Jack", "Tom").mapTo(hashSetOf<Int>()) <caret>{ w -> bar(w) }
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun bar(s: String) = s.length
|
||||
|
||||
val x = listOf("Jack", "Tom").mapTo(hashSetOf<Int>(), ::bar)
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Owner(val z: Int) {
|
||||
fun foo(y: Int) = y + z
|
||||
// Possible only in 1.1 with bound references (this::foo)
|
||||
val x = { arg: Int <caret> -> foo(arg) }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class Their {
|
||||
fun Int.foo() = "x"
|
||||
|
||||
val x = { arg: Int -> <caret>arg.foo() }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Person(val name: String)
|
||||
|
||||
val reader = { p: Person -> p.name<caret> }
|
||||
@@ -0,0 +1,3 @@
|
||||
class Person(val name: String)
|
||||
|
||||
val reader = Person::name
|
||||
@@ -0,0 +1,3 @@
|
||||
fun Int?.foo() = this?.hashCode() ?: 0
|
||||
|
||||
val x = { arg: Int? -> arg.foo() <caret>}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun Int?.foo() = this?.hashCode() ?: 0
|
||||
|
||||
val x = Int?::foo
|
||||
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
object Object {
|
||||
fun foo() = 42
|
||||
}
|
||||
|
||||
val x = { o: Object -> o.foo()<caret> }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun Int.foo(x: Int) = this - x
|
||||
|
||||
val x = { a: Int, b: Int <caret>-> a.foo(b) }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun Int.foo(x: Int) = this - x
|
||||
|
||||
val x = Int::foo
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun Int.foo(x: Int) = this - x
|
||||
|
||||
val x = { a: Int, b: Int <caret>-> b.foo(a) }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(y: Int) = y
|
||||
|
||||
val x = { arg: Int <caret>-> foo(arg) }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(y: Int) = y
|
||||
|
||||
val x = ::foo
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
// WITH_RUNTIME
|
||||
|
||||
val x = listOf(java.lang.String("")).map { <caret>it.bytes }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int, y: Int, z: Int) = x - y / z
|
||||
|
||||
val x = { a: Int, b: Int, c: Int <caret>-> foo(a, b, c) }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int, y: Int, z: Int) = x - y / z
|
||||
|
||||
val x = ::foo
|
||||
@@ -0,0 +1 @@
|
||||
val x = { arg: Int -> <caret>arg.toString() }
|
||||
@@ -0,0 +1 @@
|
||||
val x = Int::toString
|
||||
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun Int.exec(f: (Int) -> Unit) = f(this)
|
||||
|
||||
fun bar(x: Int) = x
|
||||
|
||||
fun foo() {
|
||||
2.exec {<caret> bar(it) }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class Wrapper<T>(private val x: T) {
|
||||
fun unwrap() = x
|
||||
}
|
||||
|
||||
val unwrapped = listOf(Wrapper(1), Wrapper("B")).map { <caret>it.unwrap() }
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
class Wrapper<T>(private val x: T) {
|
||||
fun unwrap() = x
|
||||
}
|
||||
|
||||
val unwrapped = listOf(Wrapper(1), Wrapper("B")).map(Wrapper<out Any>::unwrap)
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() = 42
|
||||
|
||||
val x = { arg: Int <caret>-> foo() }
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo(y: Int, z: Int) = y - z
|
||||
|
||||
val x = { second: Int, first: Int -> foo(first, second) }
|
||||
Reference in New Issue
Block a user