Delete useless assert() after simplify comparison #KT-14695 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e87f9633be
commit
8b135c12e7
+14
-1
@@ -24,10 +24,13 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
|||||||
import org.jetbrains.kotlin.idea.core.copied
|
import org.jetbrains.kotlin.idea.core.copied
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.isTrueConstant
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
|
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
|
||||||
import org.jetbrains.kotlin.types.isFlexible
|
import org.jetbrains.kotlin.types.isFlexible
|
||||||
|
|
||||||
class SimplifyBooleanWithConstantsInspection : IntentionBasedInspection<KtBinaryExpression>(SimplifyBooleanWithConstantsIntention::class)
|
class SimplifyBooleanWithConstantsInspection : IntentionBasedInspection<KtBinaryExpression>(SimplifyBooleanWithConstantsIntention::class)
|
||||||
@@ -58,7 +61,17 @@ class SimplifyBooleanWithConstantsIntention : SelfTargetingOffsetIndependentInte
|
|||||||
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||||
val topBinary = PsiTreeUtil.getTopmostParentOfType(element, KtBinaryExpression::class.java) ?: element
|
val topBinary = PsiTreeUtil.getTopmostParentOfType(element, KtBinaryExpression::class.java) ?: element
|
||||||
val simplified = toSimplifiedExpression(topBinary)
|
val simplified = toSimplifiedExpression(topBinary)
|
||||||
topBinary.replace(KtPsiUtil.safeDeparenthesize(simplified, true))
|
val result = topBinary.replaced(KtPsiUtil.safeDeparenthesize(simplified, true))
|
||||||
|
removeRedundantAssertion(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun removeRedundantAssertion(expression: KtExpression) {
|
||||||
|
val callExpression = expression.getNonStrictParentOfType<KtCallExpression>() ?: return
|
||||||
|
val fqName = callExpression.getCallableDescriptor()?.fqNameOrNull()
|
||||||
|
val valueArguments = callExpression.valueArguments
|
||||||
|
val isRedundant = fqName?.asString() == "kotlin.assert" &&
|
||||||
|
valueArguments.singleOrNull()?.getArgumentExpression().isTrueConstant()
|
||||||
|
if (isRedundant) callExpression.delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toSimplifiedExpression(expression: KtExpression): KtExpression {
|
private fun toSimplifiedExpression(expression: KtExpression): KtExpression {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters2
|
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters2
|
||||||
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.intentions.SimplifyBooleanWithConstantsIntention
|
import org.jetbrains.kotlin.idea.intentions.SimplifyBooleanWithConstantsIntention
|
||||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
@@ -35,15 +36,17 @@ class SimplifyComparisonFix(element: KtExpression, val value: Boolean) : KotlinQ
|
|||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
val element = element ?: return
|
val element = element ?: return
|
||||||
val parent = element.parent
|
|
||||||
val replacement = KtPsiFactory(element).createExpression("$value")
|
val replacement = KtPsiFactory(element).createExpression("$value")
|
||||||
element.replace(replacement)
|
val result = element.replaced(replacement)
|
||||||
|
|
||||||
val booleanExpression = parent.getNonStrictParentOfType<KtBinaryExpression>() ?: return
|
val booleanExpression = result.getNonStrictParentOfType<KtBinaryExpression>()
|
||||||
val simplifyIntention = SimplifyBooleanWithConstantsIntention()
|
val simplifyIntention = SimplifyBooleanWithConstantsIntention()
|
||||||
if (simplifyIntention.isApplicableTo(booleanExpression)) {
|
if (booleanExpression != null && simplifyIntention.isApplicableTo(booleanExpression)) {
|
||||||
simplifyIntention.applyTo(booleanExpression, editor)
|
simplifyIntention.applyTo(booleanExpression, editor)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
simplifyIntention.removeRedundantAssertion(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
assert(<caret>true || false)
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
assert(<caret>s != null)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
assert(<caret>s != null && true)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
assert(<caret>s != null && false)
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Simplify comparison" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
val s = ""
|
||||||
|
assert(false)
|
||||||
|
}
|
||||||
@@ -14681,6 +14681,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/simplifyBooleanWithConstants"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/simplifyBooleanWithConstants"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("assert.kt")
|
||||||
|
public void testAssert() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/simplifyBooleanWithConstants/assert.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("deeplyParenthesized.kt")
|
@TestMetadata("deeplyParenthesized.kt")
|
||||||
public void testDeeplyParenthesized() throws Exception {
|
public void testDeeplyParenthesized() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/simplifyBooleanWithConstants/deeplyParenthesized.kt");
|
||||||
|
|||||||
@@ -9529,6 +9529,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/simplifyComparison/somethingAndNotNull.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/simplifyComparison/somethingAndNotNull.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("withAssertion.kt")
|
||||||
|
public void testWithAssertion() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/simplifyComparison/withAssertion.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("withAssertion2.kt")
|
||||||
|
public void testWithAssertion2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/simplifyComparison/withAssertion2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("withAssertion3.kt")
|
||||||
|
public void testWithAssertion3() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/simplifyComparison/withAssertion3.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/specifyOverrideExplicitly")
|
@TestMetadata("idea/testData/quickfix/specifyOverrideExplicitly")
|
||||||
|
|||||||
Reference in New Issue
Block a user