flatMap call could be simplified to flatten(): Fix false positive with Array

#KT-33204 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-08-08 12:40:17 +09:00
committed by Dmitry Gridin
parent 7ff01b02a9
commit c0f896c96a
7 changed files with 51 additions and 0 deletions
@@ -11,8 +11,10 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.debugger.sequence.psi.receiverType
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
@@ -87,6 +89,11 @@ class SimplifiableCallInspection : AbstractKotlinInspection() {
val reference = lambdaExpression.singleStatement() ?: return null
val lambdaParameterName = lambdaExpression.singleLambdaParameterName() ?: return null
if (!reference.isNameReferenceTo(lambdaParameterName)) return null
val receiverType = callExpression.receiverType() ?: return null
if (KotlinBuiltIns.isPrimitiveArray(receiverType)) return null
if (KotlinBuiltIns.isArray(receiverType)
&& receiverType.arguments.firstOrNull()?.type?.let { KotlinBuiltIns.isArray(it) } != true
) return null
return "flatten()"
}),
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
arrayOf(listOf(1), listOf(2)).flatMap<caret> { it }
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun Array<List<Int>>.test() {
<caret>flatMap { it }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test() {
arrayOf(arrayOf(1, 2), arrayOf(3)).flatMap<caret> { it }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test() {
arrayOf(arrayOf(1, 2), arrayOf(3)).flatten()
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
// DISABLE-ERRORS
fun test() {
intArrayOf(1, 2).flatMap<caret> { it }
}
@@ -1472,6 +1472,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/collections/simplifiableCall"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("arrayFlatMap.kt")
public void testArrayFlatMap() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/arrayFlatMap.kt");
}
@TestMetadata("arrayFlatMap2.kt")
public void testArrayFlatMap2() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/arrayFlatMap2.kt");
}
@TestMetadata("arrayFlatMap3.kt")
public void testArrayFlatMap3() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/arrayFlatMap3.kt");
}
@TestMetadata("explicitLambdaParameter.kt")
public void testExplicitLambdaParameter() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/explicitLambdaParameter.kt");
@@ -1547,6 +1562,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/notOnlyReference.kt");
}
@TestMetadata("primitiveArrayFlatMap.kt")
public void testPrimitiveArrayFlatMap() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/primitiveArrayFlatMap.kt");
}
@TestMetadata("set.kt")
public void testSet() throws Exception {
runTest("idea/testData/inspectionsLocal/collections/simplifiableCall/set.kt");