SimplifiableCallChainInspection: replace .sorted().firstOrNull() with .min()

#KT-30725 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-04-07 15:56:59 +09:00
committed by Mikhail Glukhikh
parent bdac47369c
commit 3de723b8d0
18 changed files with 80 additions and 0 deletions
@@ -72,6 +72,14 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
Conversion("kotlin.collections.filter", "kotlin.collections.singleOrNull", "singleOrNull"),
Conversion("kotlin.collections.filter", "kotlin.collections.isNotEmpty", "any"),
Conversion("kotlin.collections.filter", "kotlin.collections.List.isEmpty", "none"),
Conversion("kotlin.collections.sorted", "kotlin.collections.firstOrNull", "min"),
Conversion("kotlin.collections.sorted", "kotlin.collections.lastOrNull", "max"),
Conversion("kotlin.collections.sortedDescending", "kotlin.collections.firstOrNull", "max"),
Conversion("kotlin.collections.sortedDescending", "kotlin.collections.lastOrNull", "min"),
Conversion("kotlin.collections.sortedBy", "kotlin.collections.firstOrNull", "minBy"),
Conversion("kotlin.collections.sortedBy", "kotlin.collections.lastOrNull", "maxBy"),
Conversion("kotlin.collections.sortedByDescending", "kotlin.collections.firstOrNull", "maxBy"),
Conversion("kotlin.collections.sortedByDescending", "kotlin.collections.lastOrNull", "minBy"),
Conversion("kotlin.text.filter", "kotlin.text.first", "first"),
Conversion("kotlin.text.filter", "kotlin.text.firstOrNull", "firstOrNull"),
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).<caret>sortedByDescending { it.second }.firstOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).maxBy { it.second }
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).<caret>sortedByDescending { it.second }.lastOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).minBy { it.second }
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).<caret>sortedBy { it.second }.firstOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).minBy { it.second }
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).<caret>sortedBy { it.second }.lastOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf("a" to 1, ,"c" to 3, "b" to 2).maxBy { it.second }
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).<caret>sortedDescending().firstOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).max()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).<caret>sortedDescending().lastOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).min()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).<caret>sorted().firstOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).min()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).<caret>sorted().lastOrNull()
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val x = listOf(1, 3, 2).max()
@@ -1291,6 +1291,46 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt");
}
@TestMetadata("sortedByDescendingFirstOrNull.kt")
public void testSortedByDescendingFirstOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedByDescendingFirstOrNull.kt");
}
@TestMetadata("sortedByDescendingLastOrNull.kt")
public void testSortedByDescendingLastOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedByDescendingLastOrNull.kt");
}
@TestMetadata("sortedByFirstOrNull.kt")
public void testSortedByFirstOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedByFirstOrNull.kt");
}
@TestMetadata("sortedByLastOrNull.kt")
public void testSortedByLastOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedByLastOrNull.kt");
}
@TestMetadata("sortedDescendingFirstOrNull.kt")
public void testSortedDescendingFirstOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedDescendingFirstOrNull.kt");
}
@TestMetadata("sortedDescendingLastOrNull.kt")
public void testSortedDescendingLastOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedDescendingLastOrNull.kt");
}
@TestMetadata("sortedFirstOrNull.kt")
public void testSortedFirstOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedFirstOrNull.kt");
}
@TestMetadata("sortedLastOrNull.kt")
public void testSortedLastOrNull() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/sortedLastOrNull.kt");
}
@TestMetadata("idea/testData/inspectionsLocal/collections/simplifiableCallChain/primitiveArray")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)