Add "Replace 'associate' with 'associateBy' or 'associateWith'" inspection

#KT-26269 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-01-29 19:14:44 +09:00
committed by Mikhail Glukhikh
parent 42120b93b8
commit f1e66d0654
68 changed files with 1064 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ReplaceAssociateFunctionInspection
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
arrayOf(1).<caret>associate { getKey(it) to it }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
arrayOf(1).associateBy { getKey(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
listOf(1).<caret>associate { getKey(it) to it }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
listOf(1).associateBy { getKey(it) }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
arrayOf(1).<caret>associate { getKey(it) to getValue(it) }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
arrayOf(1).associateBy({ getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { getKey(it) to getValue(it) }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateBy({ getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { i -> getKey(i) to getValue(i) }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateBy({ i -> getKey(i) }, { i -> getValue(i) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { Pair(getKey(it), getValue(it)) }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateBy({ getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate({ getKey(it) to getValue(it) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateBy({ getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { "$it" to "$it" }
}
@@ -0,0 +1,10 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associate' with 'associateBy'
// FIX: Replace with 'associateBy'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateBy({ "$it" }, { "$it" })
}
@@ -0,0 +1,12 @@
// PROBLEM: none
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate {
val key = getKey(it)
val value = getValue(it)
key to value
}
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
val destination = mutableMapOf<Long, Int>()
arrayOf(1).<caret>associateTo(destination) { getKey(it) to it }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
val destination = mutableMapOf<Long, Int>()
arrayOf(1).associateByTo(destination) { getKey(it) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
val destination = mutableMapOf<Long, Int>()
listOf(1).<caret>associateTo(destination) { getKey(it) to it }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun test() {
val destination = mutableMapOf<Long, Int>()
listOf(1).associateByTo(destination) { getKey(it) }
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
arrayOf(1).<caret>associateTo(destination) { getKey(it) to getValue(it) }
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
arrayOf(1).associateByTo(destination, { getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).<caret>associateTo(destination) { getKey(it) to getValue(it) }
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).associateByTo(destination, { getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).<caret>associateTo(destination) { i -> getKey(i) to getValue(i) }
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).associateByTo(destination, { i -> getKey(i) }, { i -> getValue(i) })
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).<caret>associateTo(destination) { Pair(getKey(it), getValue(it)) }
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).associateByTo(destination, { getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).<caret>associateTo(destination, { getKey(it) to getValue(it) })
}
@@ -0,0 +1,11 @@
// HIGHLIGHT: INFORMATION
// PROBLEM: Replace 'associateTo' with 'associateByTo'
// FIX: Replace with 'associateByTo'
// WITH_RUNTIME
fun getKey(i: Int): Long = 1L
fun getValue(i: Int): String = ""
fun test() {
val destination = mutableMapOf<Long, String>()
listOf(1).associateByTo(destination, { getKey(it) }, { getValue(it) })
}
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
arrayOf(1).<caret>associate { it to getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { it to getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith { getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associate<caret> { i -> i to getValue(i) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith { i -> getValue(i) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { Pair(it, getValue(it)) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith { getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate({ it to getValue(it) })
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith({ getValue(it) })
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { it to "$it" }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith { "$it" }
}
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
intArrayOf(1).<caret>associate { it to getValue(it) }
}
@@ -0,0 +1,12 @@
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test(b: Boolean) {
listOf(1).<caret>associate {
if (b) {
return@associate it to ""
}
it to getValue(it)
}
}
@@ -0,0 +1,11 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate {
val value = getValue(it)
it to value
}
}
@@ -0,0 +1,11 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).associateWith {
val value = getValue(it)
value
}
}
@@ -0,0 +1,13 @@
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test(b: Boolean) {
listOf(1).<caret>associate {
if (b) {
it to getValue(it)
} else {
it to ""
}
}
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
sequenceOf(1).<caret>associate { it to getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
sequenceOf(1).associateWith { getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
setOf(1).<caret>associate { it to getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: Replace 'associate' with 'associateWith'
// FIX: Replace with 'associateWith'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
setOf(1).associateWith { getValue(it) }
}
@@ -0,0 +1,8 @@
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
arrayOf(1).<caret>associateTo(destination) { it to getValue(it) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).<caret>associateTo(destination) { it to getValue(it) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).associateWithTo(destination) { getValue(it) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).<caret>associateTo(destination) { i -> i to getValue(i) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).associateWithTo(destination) { i -> getValue(i) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).<caret>associateTo(destination) { Pair(it, getValue(it)) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).associateWithTo(destination) { getValue(it) }
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).<caret>associateTo(destination, { it to getValue(it) })
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).associateWithTo(destination, { getValue(it) })
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).<caret>associateTo(destination, { it to "$it" })
}
@@ -0,0 +1,9 @@
// PROBLEM: Replace 'associateTo' with 'associateWithTo'
// FIX: Replace with 'associateWithTo'
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun associateWithTo() {
val destination = mutableMapOf<Int, String>()
listOf(1).associateWithTo(destination, { "$it" })
}
@@ -0,0 +1,8 @@
// LANGUAGE_VERSION: 1.2
// PROBLEM: none
// WITH_RUNTIME
fun getValue(i: Int): String = ""
fun test() {
listOf(1).<caret>associate { it to getValue(it) }
}