Remove setter parameter type inspection #KT-5771 Fixed

(cherry picked from commit 731a670)
This commit is contained in:
Mikhail Glukhikh
2016-07-19 18:11:23 +03:00
committed by Mikhail Glukhikh
parent 1d5924d236
commit 82e69d9587
10 changed files with 114 additions and 12 deletions
@@ -0,0 +1,6 @@
<html>
<body>
This inspection reports explicitly given parameter types in property setters.
Setter parameter type always matches property type, so it's not needed to be explicit.
</body>
</html>
+8
View File
@@ -1627,6 +1627,14 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveSetterParameterTypeInspection"
displayName="Redundant setter parameter type"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -16,26 +16,54 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class RemoveExplicitTypeIntention : SelfTargetingIntention<KtCallableDeclaration>(KtCallableDeclaration::class.java, "Remove explicit type specification") {
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
if (element.containingFile is KtCodeFragment) return false
if (element.typeReference == null) return false
class RemoveSetterParameterTypeInspection(
val intention: RemoveExplicitTypeIntention = RemoveExplicitTypeIntention()
) : IntentionBasedInspection<KtCallableDeclaration>(
intention,
{ intention.isSetterParameter(it) }
) {
override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL
val initializer = (element as? KtWithExpressionInitializer)?.initializer
if (initializer != null && initializer.textRange.containsOffset(caretOffset)) return false
override fun inspectionRange(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference?.let {
val start = it.getStartOffsetIn(element)
TextRange(start, start + it.endOffset - it.startOffset)
}
}
return when (element) {
is KtProperty -> initializer != null
is KtNamedFunction -> !element.hasBlockBody() && initializer != null
is KtParameter -> element.isLoopParameter
else -> false
class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(
KtCallableDeclaration::class.java,
"Remove explicit type specification"
) {
private val KtParameter.isSetterParameter: Boolean get() = (parent?.parent as? KtPropertyAccessor)?.isSetter ?: false
fun isSetterParameter(element: KtCallableDeclaration) =
element is KtParameter && element.isSetterParameter
override fun applicabilityRange(element: KtCallableDeclaration): TextRange? {
if (element.containingFile is KtCodeFragment) return null
if (element.typeReference == null) return null
if (element is KtParameter && (element.isLoopParameter || element.isSetterParameter)) {
return element.textRange
}
val initializer = (element as? KtWithExpressionInitializer)?.initializer ?: return null
if (element !is KtProperty && (element !is KtNamedFunction || element.hasBlockBody())) return null
return TextRange(element.startOffset, initializer.startOffset - 1)
}
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
element.setTypeReference(null)
element.typeReference = null
}
}
@@ -0,0 +1,18 @@
<problems>
<problem>
<file>test.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant setter parameter type</problem_class>
<description>Remove explicit type specification</description>
</problem>
<problem>
<file>test.kt</file>
<line>10</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant setter parameter type</problem_class>
<description>Remove explicit type specification</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.RemoveSetterParameterTypeInspection
@@ -0,0 +1,21 @@
// YES for first two, NO for last two expected
var x: String = ""
set(param: String) {
field = "$param "
}
class My {
var y: Int = 1
set(param: Int) {
field = param - 1
}
var z: Double = 3.14
private set
var w: Boolean = true
set(param) {
field = !param
}
}
@@ -0,0 +1,4 @@
var x: String = " "
set(param: String<caret>) {
field = "$param "
}
@@ -0,0 +1,4 @@
var x: String = " "
set(param) {
field = "$param "
}
@@ -220,6 +220,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
doTest(fileName);
}
@TestMetadata("removeSetterParameterType/inspectionData/inspections.test")
public void testRemoveSetterParameterType_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/removeSetterParameterType/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("spelling/inspectionData/inspections.test")
public void testSpelling_inspectionData_Inspections_test() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
@@ -8184,6 +8184,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("onSetterParameter.kt")
public void testOnSetterParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onSetterParameter.kt");
doTest(fileName);
}
@TestMetadata("onType.kt")
public void testOnType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onType.kt");