diff --git a/plugins/kotlin/idea/resources-en/inspectionDescriptions/ReplaceIsEmptyWithIfEmpty.html b/plugins/kotlin/idea/resources-en/inspectionDescriptions/ReplaceIsEmptyWithIfEmpty.html new file mode 100644 index 000000000000..2d7c0e9b1c41 --- /dev/null +++ b/plugins/kotlin/idea/resources-en/inspectionDescriptions/ReplaceIsEmptyWithIfEmpty.html @@ -0,0 +1,6 @@ + + +This inspection reports isEmpty or isBlank calls to assign a default value. +They can be replaced with ifEmpty or ifBlank calls. + + \ No newline at end of file diff --git a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties index 45b35c9ba9b8..40a165379507 100644 --- a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties +++ b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties @@ -2293,6 +2293,7 @@ title.packages.to.use.import.with=Packages to Use Import with '*' redundant.qualifier.unnecessary.non.direct.parent.class.qualifier=Unnecessary non-direct parent classes qualifiers fix.add.exception.to.throws=Add ''{0}'' fix.add.eq.eq.true=Add '== true' +inspection.replace.isempty.with.ifempty.display.name=Replace 'isEmpty' with 'ifEmpty' inspection.replace.with.ignore.case.equals.display.name=Replace with 'equals(..., ignoreCase = true)' inspection.redundant.nullable.return.type.display.name=Redundant nullable return type 0.always.returns.non.null.type=''{0}'' always returns non-null type diff --git a/plugins/kotlin/idea/resources/META-INF/inspections.xml b/plugins/kotlin/idea/resources/META-INF/inspections.xml index da1f2c943f8c..395b55c5e1d0 100644 --- a/plugins/kotlin/idea/resources/META-INF/inspections.xml +++ b/plugins/kotlin/idea/resources/META-INF/inspections.xml @@ -2599,6 +2599,14 @@ language="kotlin" key="inspection.unused.result.of.data.class.copy" bundle="messages.KotlinBundle"/> + + \ No newline at end of file diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceIsEmptyWithIfEmptyInspection.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceIsEmptyWithIfEmptyInspection.kt new file mode 100644 index 000000000000..b36a81904b66 --- /dev/null +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceIsEmptyWithIfEmptyInspection.kt @@ -0,0 +1,109 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.inspections + +import com.intellij.codeInspection.LocalQuickFix +import com.intellij.codeInspection.ProblemDescriptor +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.config.LanguageVersion +import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isElseIf +import org.jetbrains.kotlin.idea.project.languageVersionSettings +import org.jetbrains.kotlin.idea.util.textRangeIn +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.blockExpressionsOrSingle +import org.jetbrains.kotlin.psi.psiUtil.getPossiblyQualifiedCallExpression +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode + +class ReplaceIsEmptyWithIfEmptyInspection : AbstractKotlinInspection() { + private data class Replacement( + val conditionFunctionFqName: FqName, + val replacementFunctionName: String, + val negativeCondition: Boolean = false + ) + + companion object { + private val replacements = listOf( + Replacement(FqName("kotlin.collections.Collection.isEmpty"), "ifEmpty"), + Replacement(FqName("kotlin.collections.List.isEmpty"), "ifEmpty"), + Replacement(FqName("kotlin.collections.Set.isEmpty"), "ifEmpty"), + Replacement(FqName("kotlin.collections.Map.isEmpty"), "ifEmpty"), + Replacement(FqName("kotlin.text.isEmpty"), "ifEmpty"), + Replacement(FqName("kotlin.text.isBlank"), "ifBlank"), + Replacement(FqName("kotlin.collections.isNotEmpty"), "ifEmpty", negativeCondition = true), + Replacement(FqName("kotlin.text.isNotEmpty"), "ifEmpty", negativeCondition = true), + Replacement(FqName("kotlin.text.isNotBlank"), "ifBlank", negativeCondition = true), + ).associateBy { it.conditionFunctionFqName } + + private val conditionFunctionShortNames = replacements.keys.map { it.shortName().asString() }.toSet() + } + + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = ifExpressionVisitor(fun(ifExpression: KtIfExpression) { + if (ifExpression.languageVersionSettings.languageVersion < LanguageVersion.KOTLIN_1_3) return + if (ifExpression.isElseIf()) return + val thenExpression = ifExpression.then ?: return + val elseExpression = ifExpression.`else` ?: return + if (elseExpression is KtIfExpression) return + + val condition = ifExpression.condition ?: return + val conditionCallExpression = condition.getPossiblyQualifiedCallExpression() ?: return + val conditionCalleeExpression = conditionCallExpression.calleeExpression ?: return + if (conditionCalleeExpression.text !in conditionFunctionShortNames) return + + val context = ifExpression.analyze(BodyResolveMode.PARTIAL) + val resultingDescriptor = conditionCallExpression.getResolvedCall(context)?.resultingDescriptor ?: return + val receiverParameter = resultingDescriptor.dispatchReceiverParameter ?: resultingDescriptor.extensionReceiverParameter + val receiverType = receiverParameter?.type ?: return + if (KotlinBuiltIns.isArrayOrPrimitiveArray(receiverType)) return + val conditionCallFqName = resultingDescriptor.fqNameOrNull() ?: return + val replacement = replacements[conditionCallFqName] ?: return + + val selfBranch = if (replacement.negativeCondition) thenExpression else elseExpression + val selfValueExpression = selfBranch.blockExpressionsOrSingle().singleOrNull() ?: return + if (condition is KtDotQualifiedExpression) { + if (selfValueExpression.text != condition.receiverExpression.text) return + } else { + if (selfValueExpression !is KtThisExpression) return + } + + holder.registerProblem( + ifExpression, + conditionCalleeExpression.textRangeIn(ifExpression), + KotlinBundle.message("replace.with.0", "${replacement.replacementFunctionName} {...}"), + ReplaceFix(replacement) + ) + }) + + private class ReplaceFix(private val replacement: Replacement) : LocalQuickFix { + override fun getName() = KotlinBundle.message("replace.with.0", "${replacement.replacementFunctionName} {...}") + + override fun getFamilyName() = name + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + val ifExpression = descriptor.psiElement as? KtIfExpression ?: return + val condition = ifExpression.condition ?: return + val thenExpression = ifExpression.then ?: return + val elseExpression = ifExpression.`else` ?: return + val defaultValueExpression = (if (replacement.negativeCondition) elseExpression else thenExpression) + + val psiFactory = KtPsiFactory(ifExpression) + val receiverText = (condition as? KtDotQualifiedExpression)?.receiverExpression?.text?.let { "$it." } ?: "" + val replacementFunctionName = replacement.replacementFunctionName + val newExpression = if (defaultValueExpression is KtBlockExpression) { + psiFactory.createExpression("${receiverText}$replacementFunctionName ${defaultValueExpression.text}") + } else { + psiFactory.createExpressionByPattern("${receiverText}$replacementFunctionName { $0 }", defaultValueExpression) + } + ifExpression.replace(newExpression) + } + } +} diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 92241526ac8f..ec521706395d 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -10514,6 +10514,190 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty") + public static class ReplaceIsEmptyWithIfEmpty extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("defaultValueBlockHasMultiStatement.kt") + public void testDefaultValueBlockHasMultiStatement() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt"); + } + + @TestMetadata("defaultValueBlockHasMultiStatement2.kt") + public void testDefaultValueBlockHasMultiStatement2() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt"); + } + + @TestMetadata("elseIf.kt") + public void testElseIf() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/elseIf.kt"); + } + + @TestMetadata("implicitReceiver.kt") + public void testImplicitReceiver() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt"); + } + + @TestMetadata("negated.kt") + public void testNegated() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/negated.kt"); + } + + @TestMetadata("noElse.kt") + public void testNoElse() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/noElse.kt"); + } + + @TestMetadata("notSelfValue.kt") + public void testNotSelfValue() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue.kt"); + } + + @TestMetadata("notSelfValue2.kt") + public void testNotSelfValue2() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue2.kt"); + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array") + public static class Array extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection") + public static class Collection extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray") + public static class IntArray extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list") + public static class List extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map") + public static class Map extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set") + public static class Set extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str") + public static class Str extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + @TestMetadata("isBlank.kt") + public void testIsBlank() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt"); + } + + @TestMetadata("isEmpty.kt") + public void testIsEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt"); + } + + @TestMetadata("isNotBlank.kt") + public void testIsNotBlank() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt"); + } + + @TestMetadata("isNotEmpty.kt") + public void testIsNotEmpty() throws Exception { + runTest("testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt"); + } + } + } + @RunWith(JUnit3RunnerWithInners.class) @TestMetadata("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog") public abstract static class ReplaceJavaStaticMethodWithKotlinAnalog extends AbstractLocalInspectionTest { diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/.inspection b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/.inspection new file mode 100644 index 000000000000..2f9b76699352 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.ReplaceIsEmptyWithIfEmptyInspection diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isEmpty.kt new file mode 100644 index 000000000000..0d2dab7e8743 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isEmpty.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(arr: Array): Array { + return if (arr.isEmpty()) { + arrayOf("a") + } else { + arr + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isNotEmpty.kt new file mode 100644 index 000000000000..2d975dc22cd8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/array/isNotEmpty.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(arr: Array): Array { + return if (arr.isNotEmpty()) { + arr + } else { + arrayOf("a") + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt new file mode 100644 index 000000000000..a0db99714888 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(collection: Collection): Collection { + return if (collection.isEmpty()) { + listOf(1) + } else { + collection + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt.after new file mode 100644 index 000000000000..430af274aa64 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(collection: Collection): Collection { + return collection.ifEmpty { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt new file mode 100644 index 000000000000..1c5afffc5118 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(collection: Collection): Collection { + return if (collection.isNotEmpty()) { + collection + } else { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt.after new file mode 100644 index 000000000000..430af274aa64 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/collection/isNotEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(collection: Collection): Collection { + return collection.ifEmpty { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt new file mode 100644 index 000000000000..0bcdd6c384f9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt @@ -0,0 +1,11 @@ +// WITH_RUNTIME +fun foo() = listOf(1) + +fun test(list: List): List { + return if (list.isEmpty()) { + println() + foo() + } else { + list + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt.after new file mode 100644 index 000000000000..cd7d2fdf8bb2 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +fun foo() = listOf(1) + +fun test(list: List): List { + return list.ifEmpty { + println() + foo() + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt new file mode 100644 index 000000000000..76a6d00d25be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt @@ -0,0 +1,11 @@ +// WITH_RUNTIME +fun foo() = listOf(1) + +fun test(list: List): List { + return if (list.isNotEmpty()) { + list + } else { + println() + foo() + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt.after new file mode 100644 index 000000000000..cd7d2fdf8bb2 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/defaultValueBlockHasMultiStatement2.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +fun foo() = listOf(1) + +fun test(list: List): List { + return list.ifEmpty { + println() + foo() + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/elseIf.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/elseIf.kt new file mode 100644 index 000000000000..dc89a4231bc3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/elseIf.kt @@ -0,0 +1,11 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(list: List, b: Boolean): List { + return if (list.isEmpty()) { + listOf(1) + } else if (b) { + listOf(2) + } else { + list + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt new file mode 100644 index 000000000000..44f9070c79e8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun String.test(): String { + return if (isBlank()) { + "foo" + } else { + this + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt.after new file mode 100644 index 000000000000..5125e1bcc084 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/implicitReceiver.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun String.test(): String { + return ifBlank { + "foo" + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isEmpty.kt new file mode 100644 index 000000000000..5825307f5330 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isEmpty.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(intArr: IntArray): IntArray { + return if (intArr.isEmpty()) { + intArrayOf(1) + } else { + intArr + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isNotEmpty.kt new file mode 100644 index 000000000000..8e33b68a7394 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/intArray/isNotEmpty.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(intArr: IntArray): IntArray { + return if (intArr.isNotEmpty()) { + intArr + } else { + intArrayOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt new file mode 100644 index 000000000000..8b6c9ef0cbe1 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(list: List): List { + return if (list.isEmpty()) { + listOf(1) + } else { + list + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt.after new file mode 100644 index 000000000000..1c654e0fb3a4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(list: List): List { + return list.ifEmpty { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt new file mode 100644 index 000000000000..9a133854e6c6 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(list: List): List { + return if (list.isNotEmpty()) { + list + } else { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt.after new file mode 100644 index 000000000000..1c654e0fb3a4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/list/isNotEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(list: List): List { + return list.ifEmpty { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt new file mode 100644 index 000000000000..c2c112e9bf8a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(map: Map): Map { + return if (map.isEmpty()) { + mapOf(1 to 2) + } else { + map + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt.after new file mode 100644 index 000000000000..45d5226b7653 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(map: Map): Map { + return map.ifEmpty { + mapOf(1 to 2) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt new file mode 100644 index 000000000000..b262d7ee0743 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(map: Map): Map { + return if (map.isNotEmpty()) { + map + } else { + mapOf(1 to 2) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt.after new file mode 100644 index 000000000000..45d5226b7653 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/map/isNotEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(map: Map): Map { + return map.ifEmpty { + mapOf(1 to 2) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/negated.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/negated.kt new file mode 100644 index 000000000000..c412a7b27854 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/negated.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(list: List): List { + return if (!list.isEmpty()) { + list + } else { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/noElse.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/noElse.kt new file mode 100644 index 000000000000..358d7ecf2255 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/noElse.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(list: List) { + if (list.isEmpty()) { + listOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue.kt new file mode 100644 index 000000000000..2b058d40e09e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(list: List): List { + return if (list.isEmpty()) { + listOf(1) + } else { + list + list + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue2.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue2.kt new file mode 100644 index 000000000000..9d372b5bca2e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/notSelfValue2.kt @@ -0,0 +1,10 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(list: List): List { + return if (list.isEmpty()) { + listOf(1) + } else { + println() + list + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt new file mode 100644 index 000000000000..6b236a1f7f49 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(set: Set): Set { + return if (set.isEmpty()) { + setOf(1) + } else { + set + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt.after new file mode 100644 index 000000000000..5a8311ad75ff --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(set: Set): Set { + return set.ifEmpty { + setOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt new file mode 100644 index 000000000000..23e54c364dfc --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +fun test(set: Set): Set { + return if (set.isNotEmpty()) { + set + } else { + setOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt.after new file mode 100644 index 000000000000..5a8311ad75ff --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/set/isNotEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +fun test(set: Set): Set { + return set.ifEmpty { + setOf(1) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt new file mode 100644 index 000000000000..680568eaee8c --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = if (api.name.isBlank()) "John" else api.name +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt.after new file mode 100644 index 000000000000..d1e686fb5b86 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isBlank.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = api.name.ifBlank { "John" } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt new file mode 100644 index 000000000000..094b021d8383 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = if (api.name.isEmpty()) "John" else api.name +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt.after new file mode 100644 index 000000000000..dcecc4cff670 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = api.name.ifEmpty { "John" } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt new file mode 100644 index 000000000000..2c966cb30d21 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = if (api.name.isNotBlank()) + api.name + else + "John" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt.after new file mode 100644 index 000000000000..d1e686fb5b86 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotBlank.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = api.name.ifBlank { "John" } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt new file mode 100644 index 000000000000..e91a87072e28 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = if (api.name.isNotEmpty()) + api.name + else + "John" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt.after new file mode 100644 index 000000000000..dcecc4cff670 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceIsEmptyWithIfEmpty/str/isNotEmpty.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +class Api(val name: String) + +fun test(api: Api) { + val name = api.name.ifEmpty { "John" } +} \ No newline at end of file