Remove setter parameter type inspection #KT-5771 Fixed
(cherry picked from commit 731a670)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
1d5924d236
commit
82e69d9587
@@ -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>
|
||||||
@@ -1627,6 +1627,14 @@
|
|||||||
language="kotlin"
|
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"/>
|
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||||
|
|
||||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||||
|
|||||||
@@ -16,26 +16,54 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
import com.intellij.openapi.editor.Editor
|
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.*
|
||||||
|
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") {
|
class RemoveSetterParameterTypeInspection(
|
||||||
override fun isApplicableTo(element: KtCallableDeclaration, caretOffset: Int): Boolean {
|
val intention: RemoveExplicitTypeIntention = RemoveExplicitTypeIntention()
|
||||||
if (element.containingFile is KtCodeFragment) return false
|
) : IntentionBasedInspection<KtCallableDeclaration>(
|
||||||
if (element.typeReference == null) return false
|
intention,
|
||||||
|
{ intention.isSetterParameter(it) }
|
||||||
|
) {
|
||||||
|
override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||||
|
|
||||||
val initializer = (element as? KtWithExpressionInitializer)?.initializer
|
override fun inspectionRange(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference?.let {
|
||||||
if (initializer != null && initializer.textRange.containsOffset(caretOffset)) return false
|
val start = it.getStartOffsetIn(element)
|
||||||
|
TextRange(start, start + it.endOffset - it.startOffset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return when (element) {
|
class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(
|
||||||
is KtProperty -> initializer != null
|
KtCallableDeclaration::class.java,
|
||||||
is KtNamedFunction -> !element.hasBlockBody() && initializer != null
|
"Remove explicit type specification"
|
||||||
is KtParameter -> element.isLoopParameter
|
) {
|
||||||
else -> false
|
|
||||||
|
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?) {
|
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
|
||||||
element.setTypeReference(null)
|
element.typeReference = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+18
@@ -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>
|
||||||
+1
@@ -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);
|
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")
|
@TestMetadata("spelling/inspectionData/inspections.test")
|
||||||
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
public void testSpelling_inspectionData_Inspections_test() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/spelling/inspectionData/inspections.test");
|
||||||
|
|||||||
@@ -8184,6 +8184,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("onType.kt")
|
||||||
public void testOnType() throws Exception {
|
public void testOnType() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onType.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onType.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user