Add variance (in / out) modifier inspection #KT-11090 Fixed
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports type parameters that can have 'in' or 'out' variance added.
|
||||
Usage of 'in' and 'out' variances leads to more precise type inference in Kotlin.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1423,6 +1423,13 @@
|
||||
level="WEAK WARNING"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.AddVarianceModifierInspection"
|
||||
displayName="Type parameter can have 'in' or 'out' variance"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtTypeParameter
|
||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
||||
import org.jetbrains.kotlin.psi.addRemoveModifier.addModifier
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.ManualVariance
|
||||
import org.jetbrains.kotlin.resolve.VarianceCheckerCore
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
class AddVarianceModifierInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return object : KtVisitorVoid() {
|
||||
|
||||
private fun variancePossible(
|
||||
klass: KtClassOrObject,
|
||||
parameterDescriptor: TypeParameterDescriptor,
|
||||
variance: Variance,
|
||||
context: BindingContext
|
||||
) = VarianceCheckerCore(
|
||||
context,
|
||||
DiagnosticSink.DO_NOTHING,
|
||||
ManualVariance(parameterDescriptor, variance)
|
||||
).checkClassOrObject(klass)
|
||||
|
||||
override fun visitClassOrObject(klass: KtClassOrObject) {
|
||||
val context = klass.analyzeFully()
|
||||
for (typeParameter in klass.typeParameters) {
|
||||
if (typeParameter.variance != Variance.INVARIANT) continue
|
||||
val parameterDescriptor =
|
||||
context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, typeParameter) as? TypeParameterDescriptor ?: continue
|
||||
val variances = listOf(Variance.IN_VARIANCE, Variance.OUT_VARIANCE).filter {
|
||||
variancePossible(klass, parameterDescriptor, it, context)
|
||||
}
|
||||
if (variances.isNotEmpty()) {
|
||||
val suggested = variances.joinToString(" or ") { "'$it'" }
|
||||
val fixes = variances.map { AddVarianceFix(it) }
|
||||
holder.registerProblem(
|
||||
typeParameter,
|
||||
"Type parameter can have $suggested variance",
|
||||
ProblemHighlightType.WEAK_WARNING,
|
||||
*fixes.toTypedArray()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AddVarianceFix(val variance: Variance) : LocalQuickFix {
|
||||
override fun getName() = "Add '$variance' variance"
|
||||
|
||||
override fun getFamilyName() = "Add variance"
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val typeParameter = descriptor.psiElement as? KtTypeParameter
|
||||
?: throw AssertionError("Add variance fix is used on ${descriptor.psiElement.text}")
|
||||
addModifier(typeParameter, if (variance == Variance.IN_VARIANCE) KtTokens.IN_KEYWORD else KtTokens.OUT_KEYWORD)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>1</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Type parameter can have 'in' or 'out' variance</problem_class>
|
||||
<description>Type parameter can have 'out' variance</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>5</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Type parameter can have 'in' or 'out' variance</problem_class>
|
||||
<description>Type parameter can have 'in' variance</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>17</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Type parameter can have 'in' or 'out' variance</problem_class>
|
||||
<description>Type parameter can have 'out' variance</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>21</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Type parameter can have 'in' or 'out' variance</problem_class>
|
||||
<description>Type parameter can have 'in' variance</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>24</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Type parameter can have 'in' or 'out' variance</problem_class>
|
||||
<description>Type parameter can have 'in' or 'out' variance</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.AddVarianceModifierInspection
|
||||
@@ -0,0 +1,24 @@
|
||||
interface EffectivelyOut<T> {
|
||||
fun foo(): T
|
||||
val bar: T
|
||||
}
|
||||
interface EffectivelyIn<T> {
|
||||
fun foo(arg: T)
|
||||
}
|
||||
interface Invariant1<T> {
|
||||
var bar: T
|
||||
}
|
||||
interface Invariant2<T> {
|
||||
fun T.foo(): T
|
||||
}
|
||||
interface Invariant3<T : Invariant1<T>> {
|
||||
fun T.foo()
|
||||
}
|
||||
abstract class AbstractOut<T> {
|
||||
abstract val foo: T
|
||||
private var bar = foo
|
||||
}
|
||||
abstract class AbstractIn<T>(private val foo: T) {
|
||||
fun bar(arg: T) = foo == arg
|
||||
}
|
||||
interface Empty<T>
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.AddVarianceModifierInspection
|
||||
@@ -0,0 +1,2 @@
|
||||
// "Add 'out' variance" "true"
|
||||
interface Empty<<caret>T>
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add 'in' variance" "true"
|
||||
abstract class AbstractIn<<caret>T>(private val foo: T) {
|
||||
fun bar(arg: T) = foo == arg
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add 'in' variance" "true"
|
||||
abstract class AbstractIn<in T>(private val foo: T) {
|
||||
fun bar(arg: T) = foo == arg
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add 'out' variance" "true"
|
||||
abstract class AbstractOut<<caret>T> {
|
||||
abstract val foo: T
|
||||
private var bar = foo
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Add 'out' variance" "true"
|
||||
abstract class AbstractOut<out T> {
|
||||
abstract val foo: T
|
||||
private var bar = foo
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// "Add 'out' variance" "true"
|
||||
interface Empty<out T>
|
||||
@@ -102,6 +102,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Inspections extends AbstractInspectionTest {
|
||||
@TestMetadata("addVarianceModifier/inspectionData/inspections.test")
|
||||
public void testAddVarianceModifier_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/addVarianceModifier/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInInspections() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/inspections"), Pattern.compile("^(inspections\\.test)$"));
|
||||
}
|
||||
|
||||
@@ -509,6 +509,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/addVarianceModifier")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class AddVarianceModifier extends AbstractQuickFixTest {
|
||||
@TestMetadata("abstractIn.kt")
|
||||
public void testAbstractIn() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addVarianceModifier/abstractIn.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("abstractOut.kt")
|
||||
public void testAbstractOut() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addVarianceModifier/abstractOut.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInAddVarianceModifier() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addVarianceModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("empty.kt")
|
||||
public void testEmpty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addVarianceModifier/empty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/asyncUnsupported")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
@@ -7368,8 +7395,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
|
||||
@TestMetadata("letClassImplementInterfaceNullable.kt")
|
||||
public void testLetClassImplementInterfaceNullable() throws Exception {
|
||||
String fileName =
|
||||
KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementInterfaceNullable.kt");
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/letClassImplementInterfaceNullable.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user