FIR IDE: Remove multiple nested params in RemoveUselessElvisFix and

RemoveUselessCastFix.
This commit is contained in:
Mark Punzalan
2021-05-26 20:52:52 +00:00
committed by TeamCityServer
parent a778cc673e
commit d12a24418e
15 changed files with 148 additions and 6 deletions
@@ -561,6 +561,16 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/expressions/removeUselessCast.kt");
}
@TestMetadata("removeUselessCastForLambdaInNecessaryNestedParens2.kt")
public void testRemoveUselessCastForLambdaInNecessaryNestedParens2() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt");
}
@TestMetadata("removeUselessCastForLambdaInNestedParens.kt")
public void testRemoveUselessCastForLambdaInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt");
}
@TestMetadata("removeUselessCastForLambdaInParens1.kt")
public void testRemoveUselessCastForLambdaInParens1() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt");
@@ -591,6 +601,11 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt");
}
@TestMetadata("removeUselessCastInNestedParens.kt")
public void testRemoveUselessCastInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt");
}
@TestMetadata("removeUselessCastInParens.kt")
public void testRemoveUselessCastInParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastInParens.kt");
@@ -691,6 +706,21 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
}
@TestMetadata("uselessElvisForLambdaInNecessaryNestedParens.kt")
public void testUselessElvisForLambdaInNecessaryNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisForLambdaInNecessaryNestedParens.kt");
}
@TestMetadata("uselessElvisForLambdaInNestedParens.kt")
public void testUselessElvisForLambdaInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisForLambdaInNestedParens.kt");
}
@TestMetadata("uselessElvisInNestedParens.kt")
public void testUselessElvisInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisInNestedParens.kt");
}
@TestMetadata("uselessElvisRightIsNull.kt")
public void testUselessElvisRightIsNull() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisRightIsNull.kt");
@@ -11,14 +11,21 @@ import org.jetbrains.kotlin.psi.KtParenthesizedExpression
import org.jetbrains.kotlin.psi.KtPsiUtil
inline fun <reified T : PsiElement> PsiElement.replaced(newElement: T): T {
if (this == newElement) return newElement
val result = replace(newElement)
return result as? T ?: (result as KtParenthesizedExpression).expression as T
}
fun KtExpression.dropEnclosingParenthesesIfPossible(): KtExpression {
val parent = parent as? KtParenthesizedExpression ?: return this
if (!KtPsiUtil.areParenthesesUseless(parent)) return this
return parent.replaced(this)
val innermostExpression = this
var current = innermostExpression
while (true) {
val parent = current.parent as? KtParenthesizedExpression ?: break
if (!KtPsiUtil.areParenthesesUseless(parent)) break
current = parent
}
return current.replaced(innermostExpression)
}
//todo make inline
@@ -26,6 +33,3 @@ fun KtExpression.dropEnclosingParenthesesIfPossible(): KtExpression {
fun <T : PsiElement> T.copied(): T = copy() as T
fun String.unquote(): String = KtPsiUtil.unquoteIdentifier(this)
@@ -0,0 +1,9 @@
// "Remove useless cast" "true"
fun foo() {}
fun test() {
foo()
// comment
((({ "" } as<caret> () -> String)))
}
/* IGNORE_FIR */
@@ -0,0 +1,9 @@
// "Remove useless cast" "true"
fun foo() {}
fun test() {
foo()
// comment
({ "" })
}
/* IGNORE_FIR */
@@ -0,0 +1,5 @@
// "Remove useless cast" "true"
fun test() {
((({ "" } as<caret> () -> String)))
}
/* IGNORE_FIR */
@@ -0,0 +1,5 @@
// "Remove useless cast" "true"
fun test() {
{ "" }
}
/* IGNORE_FIR */
@@ -0,0 +1,9 @@
// "Remove useless cast" "true"
fun test(x: Any): Int {
if (x is String) {
return (((x <caret>as String))).length
}
return -1
}
/* IGNORE_FIR */
@@ -0,0 +1,9 @@
// "Remove useless cast" "true"
fun test(x: Any): Int {
if (x is String) {
return x.length
}
return -1
}
/* IGNORE_FIR */
@@ -0,0 +1,8 @@
// "Remove useless elvis operator" "true"
fun foo() {}
fun test() {
foo()
// comment
((({ "" } <caret>?: null)))
}
@@ -0,0 +1,8 @@
// "Remove useless elvis operator" "true"
fun foo() {}
fun test() {
foo()
// comment
({ "" })
}
@@ -0,0 +1,4 @@
// "Remove useless elvis operator" "true"
fun test() {
((({ "" } <caret>?: null)))
}
@@ -0,0 +1,4 @@
// "Remove useless elvis operator" "true"
fun test() {
{ "" }
}
@@ -0,0 +1,4 @@
// "Remove useless elvis operator" "true"
fun foo(a: String) {
val b : String = (((a <caret>?: "s")))
}
@@ -0,0 +1,4 @@
// "Remove useless elvis operator" "true"
fun foo(a: String) {
val b : String = a<caret>
}
@@ -7750,6 +7750,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/expressions/removeUselessCast.kt");
}
@TestMetadata("removeUselessCastForLambdaInNecessaryNestedParens2.kt")
public void testRemoveUselessCastForLambdaInNecessaryNestedParens2() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt");
}
@TestMetadata("removeUselessCastForLambdaInNestedParens.kt")
public void testRemoveUselessCastForLambdaInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt");
}
@TestMetadata("removeUselessCastForLambdaInParens1.kt")
public void testRemoveUselessCastForLambdaInParens1() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt");
@@ -7780,6 +7790,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt");
}
@TestMetadata("removeUselessCastInNestedParens.kt")
public void testRemoveUselessCastInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt");
}
@TestMetadata("removeUselessCastInParens.kt")
public void testRemoveUselessCastInParens() throws Exception {
runTest("idea/testData/quickfix/expressions/removeUselessCastInParens.kt");
@@ -7880,6 +7895,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/expressions/uselessElvis.kt");
}
@TestMetadata("uselessElvisForLambdaInNecessaryNestedParens.kt")
public void testUselessElvisForLambdaInNecessaryNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisForLambdaInNecessaryNestedParens.kt");
}
@TestMetadata("uselessElvisForLambdaInNestedParens.kt")
public void testUselessElvisForLambdaInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisForLambdaInNestedParens.kt");
}
@TestMetadata("uselessElvisInNestedParens.kt")
public void testUselessElvisInNestedParens() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisInNestedParens.kt");
}
@TestMetadata("uselessElvisRightIsNull.kt")
public void testUselessElvisRightIsNull() throws Exception {
runTest("idea/testData/quickfix/expressions/uselessElvisRightIsNull.kt");