diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantGetterInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantGetterInspection.kt index 43766544da4..cc8c585d091 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantGetterInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantGetterInspection.kt @@ -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() } } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasType.kt b/idea/testData/inspectionsLocal/redundantGetter/hasType.kt new file mode 100644 index 00000000000..8089a3c8789 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasType.kt @@ -0,0 +1,4 @@ +interface Test { + val foo + get(): Int +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasType.kt.after b/idea/testData/inspectionsLocal/redundantGetter/hasType.kt.after new file mode 100644 index 00000000000..2ea82dc2289 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasType.kt.after @@ -0,0 +1,3 @@ +interface Test { + val foo: Int +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt new file mode 100644 index 00000000000..45415c5946d --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt @@ -0,0 +1,4 @@ +interface Test { + val foo: Int + get(): Int +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt.after b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt.after new file mode 100644 index 00000000000..2ea82dc2289 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyExplicitType.kt.after @@ -0,0 +1,3 @@ +interface Test { + val foo: Int +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt new file mode 100644 index 00000000000..f8822d6d446 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt @@ -0,0 +1,4 @@ +class Test { + val foo = 1 + get(): Int = field +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt.after b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt.after new file mode 100644 index 00000000000..2c36bfe015b --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantGetter/hasTypeWithPropertyInitializer.kt.after @@ -0,0 +1,3 @@ +class Test { + val foo = 1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index b0a09390208..544c223f50a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -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");