diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt index 3a3b0c21145..9f381a4e687 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveSetterParameterTypeInspection.kt @@ -30,12 +30,11 @@ class RemoveSetterParameterTypeInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : KtVisitorVoid() { override fun visitDeclaration(dcl: KtDeclaration) { - if (dcl is KtParameter && dcl.typeReference != null && dcl.isSetterParameter) { - holder.registerProblem(dcl, - "Redundant setter parameter type", - ProblemHighlightType.LIKE_UNUSED_SYMBOL, - IntentionWrapper(RemoveExplicitTypeIntention(), dcl.containingKtFile)) - } + val typeReference = (dcl as? KtParameter)?.takeIf { it.isSetterParameter }?.typeReference ?: return + holder.registerProblem(typeReference, + "Redundant setter parameter type", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + IntentionWrapper(RemoveExplicitTypeIntention(), dcl.containingKtFile)) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index b863adbff66..b335106f371 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -41,22 +41,23 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention TextRange(element.startOffset, initializer.startOffset - 1) - element is KtProperty && element.getter != null -> TextRange(element.startOffset, element.typeReference!!.endOffset) - element is KtNamedFunction -> TextRange(element.startOffset, element.typeReference!!.endOffset) + element is KtProperty && element.getter != null -> TextRange(element.startOffset, typeReference.endOffset) + element is KtNamedFunction -> TextRange(element.startOffset, typeReference.endOffset) else -> null } } diff --git a/idea/testData/intentions/removeExplicitType/notOnSetterParameter.kt b/idea/testData/intentions/removeExplicitType/notOnSetterParameter.kt new file mode 100644 index 00000000000..96aee6f2d47 --- /dev/null +++ b/idea/testData/intentions/removeExplicitType/notOnSetterParameter.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false +var x: String = " " + set(param: String) { + field = "$param " + } diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index cd8a8c985ab..924d2663e6d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -12701,6 +12701,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("notOnSetterParameter.kt") + public void testNotOnSetterParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnSetterParameter.kt"); + doTest(fileName); + } + @TestMetadata("onOverride.kt") public void testOnOverride() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onOverride.kt");