Replace with explicit "get" inspection enabled by default but is reported only for functions explicitly marked with "operator"
This commit is contained in:
@@ -1099,8 +1099,8 @@
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.conventionNameCalls.ExplicitGetInspection"
|
||||
displayName="Explicit 'get'"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="false"
|
||||
level="WEAK WARNING"
|
||||
enabledByDefault="true"
|
||||
level="INFO"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisInspection"
|
||||
|
||||
+12
-1
@@ -25,12 +25,23 @@ import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
||||
import org.jetbrains.kotlin.idea.intentions.isReceiverExpressionWithValue
|
||||
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
||||
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
import org.jetbrains.kotlin.psi.buildExpression
|
||||
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
|
||||
|
||||
public class ExplicitGetInspection : IntentionBasedInspection<JetDotQualifiedExpression>(ReplaceGetIntention())
|
||||
public class ExplicitGetInspection : IntentionBasedInspection<JetDotQualifiedExpression>(
|
||||
ReplaceGetIntention(),
|
||||
additionalChecker = { expression -> (expression.toResolvedCall()!!.resultingDescriptor as FunctionDescriptor).isExplicitOperator() }
|
||||
)
|
||||
|
||||
private fun FunctionDescriptor.isExplicitOperator(): Boolean {
|
||||
return if (overriddenDescriptors.isEmpty())
|
||||
containingDeclaration !is JavaClassDescriptor && isOperator
|
||||
else
|
||||
overriddenDescriptors.any { it.isExplicitOperator() }
|
||||
}
|
||||
|
||||
public class ReplaceGetIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'get' call with index operator"), HighPriorityAction {
|
||||
override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.List;
|
||||
|
||||
public abstract class JavaMethods implements List<String> {
|
||||
@Override
|
||||
public String get(int i) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int get(String s, int p) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+9
@@ -61,4 +61,13 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get'</problem_class>
|
||||
<description>Replace 'get' call with index operator</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>javaMethods.kt</file>
|
||||
<line>6</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="javaMethods.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Explicit 'get'</problem_class>
|
||||
<description>Replace 'get' call with index operator</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// DISABLE-ERRORS
|
||||
// IS_APPLICABLE: false
|
||||
import JavaMethods
|
||||
|
||||
fun foo(javaClass: JavaMethods) {
|
||||
val v1 = javaClass.get(1)
|
||||
val v2 = javaClass.get("a", 1)
|
||||
}
|
||||
@@ -2765,6 +2765,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("javaMethods.kt")
|
||||
public void testJavaMethods() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missingDefaultArgument.kt")
|
||||
public void testMissingDefaultArgument() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGet/missingDefaultArgument.kt");
|
||||
|
||||
Reference in New Issue
Block a user