Transfer type to parent property in "Remove redundant getter" inspection

So #KT-21612 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-12-06 05:20:18 +03:00
committed by Mikhail Glukhikh
parent 834c3fe62b
commit b23d62c760
8 changed files with 47 additions and 0 deletions
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RedundantGetterInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
@@ -61,6 +62,13 @@ private class RemoveRedundantGetterFix : LocalQuickFix {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val accessor = descriptor.psiElement as? KtPropertyAccessor ?: return
val property = accessor.property
val accessorTypeReference = accessor.returnTypeReference
if (accessorTypeReference != null && property.typeReference == null && property.initializer == null) {
property.typeReference = accessorTypeReference
}
accessor.delete()
}
}
@@ -0,0 +1,4 @@
interface Test {
val foo
<caret>get(): Int
}
@@ -0,0 +1,3 @@
interface Test {
val foo: Int
}
@@ -0,0 +1,4 @@
interface Test {
val foo: Int
<caret>get(): Int
}
@@ -0,0 +1,3 @@
interface Test {
val foo: Int
}
@@ -0,0 +1,4 @@
class Test {
val foo = 1
<caret>get(): Int = field
}
@@ -0,0 +1,3 @@
class Test {
val foo = 1
}
@@ -1694,6 +1694,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
@TestMetadata("hasType.kt")
public void testHasType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/hasType.kt");
doTest(fileName);
}
@TestMetadata("hasTypeWithPropertyExplicitType.kt")
public void testHasTypeWithPropertyExplicitType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt");
doTest(fileName);
}
@TestMetadata("hasTypeWithPropertyInitializer.kt")
public void testHasTypeWithPropertyInitializer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt");
doTest(fileName);
}
@TestMetadata("notFieldExpression.kt")
public void testNotFieldExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantGetter/notFieldExpression.kt");