Remove unused variable with constant initializer #KT-13886 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-07-21 13:02:42 +03:00
parent a08f55cf99
commit 9781d1fcdf
6 changed files with 32 additions and 5 deletions
@@ -21,10 +21,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtTypeArgumentList
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
open class RemovePsiElementSimpleFix(element: PsiElement, private val text: String) : KotlinQuickFixAction<PsiElement>(element) {
@@ -65,7 +62,7 @@ open class RemovePsiElementSimpleFix(element: PsiElement, private val text: Stri
return object : RemovePsiElementSimpleFix(expression, "Remove variable '${expression.name}'") {
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val initializer = expression.initializer
if (initializer != null) {
if (initializer != null && initializer !is KtConstantExpression) {
expression.replace(initializer)
}
else {
@@ -0,0 +1,5 @@
// "Remove variable 'flag'" "true"
fun foo() {
val <caret>flag = true
}
@@ -0,0 +1,4 @@
// "Remove variable 'flag'" "true"
fun foo() {
}
@@ -0,0 +1,5 @@
// "Remove variable 'i'" "true"
fun foo() {
val <caret>i: Int? = null
}
@@ -0,0 +1,4 @@
// "Remove variable 'i'" "true"
fun foo() {
}
@@ -11366,12 +11366,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/variables"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("unusedVariableWithConstantInitializer.kt")
public void testUnusedVariableWithConstantInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/unusedVariableWithConstantInitializer.kt");
doTest(fileName);
}
@TestMetadata("unusedVariableWithInitializer.kt")
public void testUnusedVariableWithInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/unusedVariableWithInitializer.kt");
doTest(fileName);
}
@TestMetadata("unusedVariableWithNullInitializer.kt")
public void testUnusedVariableWithNullInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/unusedVariableWithNullInitializer.kt");
doTest(fileName);
}
@TestMetadata("unusedVariableWithoutInitializer.kt")
public void testUnusedVariableWithoutInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/unusedVariableWithoutInitializer.kt");