Add inspection for converting !collection.isEmpty() -> isNotEmpty()
#KT-27556 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
538a746df9
commit
7b43d5c972
@@ -3196,6 +3196,15 @@
|
|||||||
language="kotlin"
|
language="kotlin"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceNegatedIsEmptyWithIsNotEmptyInspection"
|
||||||
|
displayName="Replace negated 'isEmpty' with 'isNotEmpty'"
|
||||||
|
groupPath="Kotlin"
|
||||||
|
groupName="Style issues"
|
||||||
|
enabledByDefault="true"
|
||||||
|
level="WEAK WARNING"
|
||||||
|
language="kotlin"
|
||||||
|
/>
|
||||||
|
|
||||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This inspection reports <b>!collection.isEmpty()</b> call can be replaced with <b>collection.isNotEmpty()</b>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2018 JetBrains s.r.o. 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.ProblemHighlightType
|
||||||
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.PsiElementVisitor
|
||||||
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
|
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
|
||||||
|
|
||||||
|
class ReplaceNegatedIsEmptyWithIsNotEmptyInspection : AbstractKotlinInspection() {
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||||
|
|
||||||
|
return simpleNameExpressionVisitor { simpleNameExpression ->
|
||||||
|
if (simpleNameExpression.isEmptyNegation()) {
|
||||||
|
holder.registerProblem(
|
||||||
|
simpleNameExpression,
|
||||||
|
"Replace negated 'isEmpty' with 'isNotEmpty'",
|
||||||
|
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
|
ReplaceNegatedIsEmptyWithIsNotEmptyQuickFix()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReplaceNegatedIsEmptyWithIsNotEmptyQuickFix : LocalQuickFix {
|
||||||
|
override fun getName() = "Replace negated 'isEmpty' with 'isNotEmpty'"
|
||||||
|
|
||||||
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
val element = descriptor.psiElement as? KtSimpleNameExpression ?: return
|
||||||
|
val callExpression = (element.parent as? KtCallExpression) ?: return
|
||||||
|
val qualifiedExpression = (callExpression.parent as? KtDotQualifiedExpression) ?: return
|
||||||
|
val prefixExpression = qualifiedExpression.getWrappingPrefixExpressionIfAny() ?: return
|
||||||
|
|
||||||
|
prefixExpression.replaced(
|
||||||
|
KtPsiFactory(element).createExpressionByPattern(
|
||||||
|
"$0.isNotEmpty()",
|
||||||
|
qualifiedExpression.receiverExpression
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun KtSimpleNameExpression.isEmptyNegation(): Boolean {
|
||||||
|
val callExpression = (parent as? KtCallExpression) ?: return false
|
||||||
|
val qualifiedExpression = (callExpression.parent as? KtDotQualifiedExpression) ?: return false
|
||||||
|
val prefixExpression = qualifiedExpression.getWrappingPrefixExpressionIfAny() ?: return false
|
||||||
|
if (prefixExpression.operationToken != KtTokens.EXCL) return false
|
||||||
|
return transformations.any { callExpression.isCalling(FqName(it)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun PsiElement.getWrappingPrefixExpressionIfAny() =
|
||||||
|
(getLastParentOfTypeInRow<KtParenthesizedExpression>() ?: this).parent as? KtPrefixExpression
|
||||||
|
|
||||||
|
private val transformations = listOf(
|
||||||
|
"java.util.ArrayList.isEmpty",
|
||||||
|
"java.util.HashMap.isEmpty",
|
||||||
|
"java.util.HashSet.isEmpty",
|
||||||
|
"java.util.LinkedHashMap.isEmpty",
|
||||||
|
"java.util.LinkedHashSet.isEmpty",
|
||||||
|
"kotlin.collections.isEmpty",
|
||||||
|
"kotlin.collections.List.isEmpty",
|
||||||
|
"kotlin.collections.Set.isEmpty",
|
||||||
|
"kotlin.collections.Map.isEmpty",
|
||||||
|
"kotlin.collections.MutableList.isEmpty",
|
||||||
|
"kotlin.collections.MutableSet.isEmpty",
|
||||||
|
"kotlin.collections.MutableMap.isEmpty"
|
||||||
|
)
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.inspections.ReplaceNegatedIsEmptyWithIsNotEmptyInspection
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// FIX: Replace negated 'isEmpty' with 'isNotEmpty'
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val list = listOf(1,2,3)
|
||||||
|
if (!list.<caret>isEmpty()) {
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// FIX: Replace negated 'isEmpty' with 'isNotEmpty'
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val list = listOf(1,2,3)
|
||||||
|
if (list.isNotEmpty()) {
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
@@ -6036,6 +6036,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ReplaceNegatedIsEmptyWithIsNotEmpty extends AbstractLocalInspectionTest {
|
||||||
|
private void runTest(String testDataFilePath) throws Exception {
|
||||||
|
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAllFilesPresentInReplaceNegatedIsEmptyWithIsNotEmpty() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty/simple.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/inspectionsLocal/replacePutWithAssignment")
|
@TestMetadata("idea/testData/inspectionsLocal/replacePutWithAssignment")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user