Remove ConvertIfWithThrowToAssertIntention
Relates to #KT-31502
This commit is contained in:
@@ -1138,11 +1138,6 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
<intentionAction>
|
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.ConvertIfWithThrowToAssertIntention</className>
|
|
||||||
<category>Kotlin</category>
|
|
||||||
</intentionAction>
|
|
||||||
|
|
||||||
<intentionAction>
|
<intentionAction>
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.SpecifyExplicitLambdaSignatureIntention</className>
|
<className>org.jetbrains.kotlin.idea.intentions.SpecifyExplicitLambdaSignatureIntention</className>
|
||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
|
|||||||
-3
@@ -1,3 +0,0 @@
|
|||||||
fun foo() {
|
|
||||||
<spot>assert(!true, "text")</spot>
|
|
||||||
}
|
|
||||||
-5
@@ -1,5 +0,0 @@
|
|||||||
fun foo() {
|
|
||||||
<spot>if (true) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}</spot>
|
|
||||||
}
|
|
||||||
-5
@@ -1,5 +0,0 @@
|
|||||||
<html>
|
|
||||||
<body>
|
|
||||||
This intention converts an <b>if</b> statement that throws an <b>AssertionError</b> exception into an assertion.
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions
|
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
|
||||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
|
||||||
import org.jetbrains.kotlin.idea.inspections.SimplifyNegatedBinaryExpressionInspection
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isNullExpression
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.unwrapBlockOrParenthesis
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.constant
|
|
||||||
|
|
||||||
class ConvertIfWithThrowToAssertIntention : SelfTargetingOffsetIndependentIntention<KtIfExpression>(KtIfExpression::class.java, "Replace 'if' with 'assert' statement") {
|
|
||||||
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
|
||||||
if (element.`else` != null) return false
|
|
||||||
|
|
||||||
val throwExpr = element.then?.unwrapBlockOrParenthesis() as? KtThrowExpression
|
|
||||||
val thrownExpr = getSelector(throwExpr?.thrownExpression)
|
|
||||||
if (thrownExpr !is KtCallExpression) return false
|
|
||||||
|
|
||||||
if (thrownExpr.valueArguments.size > 1) return false
|
|
||||||
|
|
||||||
val resolvedCall = thrownExpr.resolveToCall() ?: return false
|
|
||||||
val targetFqName = DescriptorUtils.getFqName(resolvedCall.resultingDescriptor).asString()
|
|
||||||
return targetFqName in constant { setOf("kotlin.AssertionError.<init>", "java.lang.AssertionError.<init>") }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
|
||||||
val condition = element.condition ?: return
|
|
||||||
|
|
||||||
val thenExpr = element.then?.unwrapBlockOrParenthesis() as KtThrowExpression
|
|
||||||
val thrownExpr = getSelector(thenExpr.thrownExpression) as KtCallExpression
|
|
||||||
|
|
||||||
val psiFactory = KtPsiFactory(element)
|
|
||||||
condition.replace(psiFactory.createExpressionByPattern("!$0", condition))
|
|
||||||
|
|
||||||
var newCondition = element.condition!!
|
|
||||||
val simplifier = SimplifyNegatedBinaryExpressionInspection()
|
|
||||||
if (simplifier.isApplicable(newCondition as KtPrefixExpression)) {
|
|
||||||
simplifier.applyTo(newCondition.operationReference, editor = editor)
|
|
||||||
newCondition = element.condition!!
|
|
||||||
}
|
|
||||||
|
|
||||||
val arg = thrownExpr.valueArguments.singleOrNull()?.getArgumentExpression()
|
|
||||||
val assertExpr = if (arg != null && !arg.isNullExpression())
|
|
||||||
psiFactory.createExpressionByPattern("kotlin.assert($0) {$1}", newCondition, arg)
|
|
||||||
else
|
|
||||||
psiFactory.createExpressionByPattern("kotlin.assert($0)", newCondition)
|
|
||||||
|
|
||||||
val newExpr = element.replaced(assertExpr)
|
|
||||||
ShortenReferences.DEFAULT.process(newExpr)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getSelector(element: KtExpression?): KtExpression? {
|
|
||||||
if (element is KtDotQualifiedExpression) {
|
|
||||||
return element.selectorExpression
|
|
||||||
}
|
|
||||||
return element
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
org.jetbrains.kotlin.idea.intentions.ConvertIfWithThrowToAssertIntention
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun assert(x: Boolean, y: Any) {}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
kotlin.assert(!true) { "text" }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun assert(x: Boolean, y: Any) {}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
package foo.kotlin
|
|
||||||
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun assert(x: Boolean, y: Any) {}
|
|
||||||
-8
@@ -1,8 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
package foo.kotlin
|
|
||||||
|
|
||||||
fun foo() {
|
|
||||||
kotlin.assert(!true) { "text" }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun assert(x: Boolean, y: Any) {}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
val x = true
|
|
||||||
if <caret>(x && false) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
val x = true
|
|
||||||
assert(!(x && false)) { "text" }
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw java.lang.AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
assert(!true) { "text" }
|
|
||||||
}
|
|
||||||
Vendored
-9
@@ -1,9 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AssertionError(x: String): Exception(x) {}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
// MIN_JAVA_VERSION: 1.7
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
throw AssertionError("text", Exception())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
} else {
|
|
||||||
val x = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Vendored
-8
@@ -1,8 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
val y = 1
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Vendored
-8
@@ -1,8 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
val y = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw AssertionError()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
assert(!true)
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw AssertionError(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
assert(!true)
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(true) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
assert(!true) { "text" }
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
if <caret>(1 == 0) {
|
|
||||||
throw AssertionError("text")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-4
@@ -1,4 +0,0 @@
|
|||||||
// WITH_RUNTIME
|
|
||||||
fun foo() {
|
|
||||||
assert(1 != 0) { "text" }
|
|
||||||
}
|
|
||||||
@@ -5328,84 +5328,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/convertIfWithThrowToAssert")
|
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
|
||||||
public static class ConvertIfWithThrowToAssert extends AbstractIntentionTest {
|
|
||||||
private void runTest(String testDataFilePath) throws Exception {
|
|
||||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testAllFilesPresentInConvertIfWithThrowToAssert() throws Exception {
|
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertIfWithThrowToAssert"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("assertOverloaded.kt")
|
|
||||||
public void testAssertOverloaded() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/assertOverloaded.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("assertOverloaded2.kt")
|
|
||||||
public void testAssertOverloaded2() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/assertOverloaded2.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("booleanCondition.kt")
|
|
||||||
public void testBooleanCondition() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/booleanCondition.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("dotQualifiedThrow.kt")
|
|
||||||
public void testDotQualifiedThrow() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/dotQualifiedThrow.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("inapplicableAssertionErrorOverloaded.kt")
|
|
||||||
public void testInapplicableAssertionErrorOverloaded() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/inapplicableAssertionErrorOverloaded.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("inapplicableCauseSent.kt")
|
|
||||||
public void testInapplicableCauseSent() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/inapplicableCauseSent.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("inapplicableHasElse.kt")
|
|
||||||
public void testInapplicableHasElse() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/inapplicableHasElse.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("inapplicableMoreThanSingleExpression.kt")
|
|
||||||
public void testInapplicableMoreThanSingleExpression() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/inapplicableMoreThanSingleExpression.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("inapplicableMoreThanSingleExpression2.kt")
|
|
||||||
public void testInapplicableMoreThanSingleExpression2() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/inapplicableMoreThanSingleExpression2.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("noMessageSent.kt")
|
|
||||||
public void testNoMessageSent() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/noMessageSent.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("nullSent.kt")
|
|
||||||
public void testNullSent() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/nullSent.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("simpleConvert.kt")
|
|
||||||
public void testSimpleConvert() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/simpleConvert.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("simplifiedCondition.kt")
|
|
||||||
public void testSimplifiedCondition() throws Exception {
|
|
||||||
runTest("idea/testData/intentions/convertIfWithThrowToAssert/simplifiedCondition.kt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/convertLambdaToReference")
|
@TestMetadata("idea/testData/intentions/convertLambdaToReference")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user