From 512e287f3ac7572202d46063201e0a9d62fd9199 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 18 Jan 2018 15:02:55 +0300 Subject: [PATCH] Remove setter parameter type: do not suggest if type is empty So #KT-22339 Fixed --- .../RemoveSetterParameterTypeInspection.kt | 10 +++- .../removeSetterParameterType/.inspection | 1 + .../removeSetterParameterType/emptyType.kt | 7 +++ .../LocalInspectionTestGenerated.java | 57 ++++++++++++------- 4 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 idea/testData/inspectionsLocal/removeSetterParameterType/.inspection create mode 100644 idea/testData/inspectionsLocal/removeSetterParameterType/emptyType.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt index 99e3f48af7f..c5eef013e34 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt @@ -12,16 +12,20 @@ import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention import org.jetbrains.kotlin.idea.intentions.isSetterParameter import org.jetbrains.kotlin.psi.parameterVisitor +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset class RemoveSetterParameterTypeInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { - return parameterVisitor { dcl -> - val typeReference = dcl.takeIf { it.isSetterParameter }?.typeReference ?: return@parameterVisitor + return parameterVisitor { parameter -> + val typeReference = parameter.takeIf { it.isSetterParameter } + ?.typeReference + ?.takeIf { it.endOffset > it.startOffset } ?: return@parameterVisitor holder.registerProblem( typeReference, "Redundant setter parameter type", ProblemHighlightType.LIKE_UNUSED_SYMBOL, - IntentionWrapper(RemoveExplicitTypeIntention(), dcl.containingKtFile) + IntentionWrapper(RemoveExplicitTypeIntention(), parameter.containingKtFile) ) } } diff --git a/idea/testData/inspectionsLocal/removeSetterParameterType/.inspection b/idea/testData/inspectionsLocal/removeSetterParameterType/.inspection new file mode 100644 index 00000000000..0aadf2a11b5 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeSetterParameterType/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.RemoveSetterParameterTypeInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeSetterParameterType/emptyType.kt b/idea/testData/inspectionsLocal/removeSetterParameterType/emptyType.kt new file mode 100644 index 00000000000..bbff89be6ff --- /dev/null +++ b/idea/testData/inspectionsLocal/removeSetterParameterType/emptyType.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +// SKIP_ERRORS_BEFORE +// SKIP_ERRORS_AFTER + +var x: String + get() = "123" + set(v: ) {} \ 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 f0379b91e96..e7b662c035a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -3100,6 +3100,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/removeSetterParameterType") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveSetterParameterType extends AbstractLocalInspectionTest { + public void testAllFilesPresentInRemoveSetterParameterType() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/removeSetterParameterType"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("emptyType.kt") + public void testEmptyType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeSetterParameterType/emptyType.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/inspectionsLocal/removeToStringInStringTemplate") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -4060,27 +4075,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } - @TestMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class UnusedLambdaExpressionBody extends AbstractLocalInspectionTest { - public void testAllFilesPresentInUnusedLambdaExpressionBody() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unusedLambdaExpressionBody"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); - } - - @TestMetadata("inEnumEntry.kt") - public void testInEnumEntry() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/inEnumEntry.kt"); - doTest(fileName); - } - - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/simple.kt"); - doTest(fileName); - } - } - @TestMetadata("idea/testData/inspectionsLocal/unsafeCastFromDynamic") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -4108,6 +4102,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class UnusedLambdaExpressionBody extends AbstractLocalInspectionTest { + public void testAllFilesPresentInUnusedLambdaExpressionBody() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unusedLambdaExpressionBody"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("inEnumEntry.kt") + public void testInEnumEntry() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/inEnumEntry.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)