Minor corrections on code review
This commit is contained in:
@@ -26,29 +26,30 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.types.typeUtil.*
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.GET
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.SET
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.INVOKE
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.CONTAINS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.ITERATOR
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.NEXT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.HAS_NEXT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.EQUALS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.COMPARE_TO
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.BINARY_OPERATION_NAMES
|
||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.ASSIGNMENT_OPERATIONS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.BINARY_OPERATION_NAMES
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.COMPARE_TO
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.COMPONENT_REGEX
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.PLUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.MINUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.RANGE_TO
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.UNARY_PLUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.UNARY_MINUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.NOT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.INC
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.CONTAINS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.DEC
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.EQUALS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.GET
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.GET_VALUE
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.HAS_NEXT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.INC
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.INVOKE
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.ITERATOR
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.MINUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.NEXT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.NOT
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.PLUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.RANGE_TO
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.SET
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.SET_VALUE
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.UNARY_MINUS
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions.UNARY_PLUS
|
||||
|
||||
object OperatorChecks {
|
||||
fun canBeOperator(functionDescriptor: FunctionDescriptor): Boolean {
|
||||
@@ -127,4 +128,6 @@ object OperatorChecks {
|
||||
private val FunctionDescriptor.noDefaultsAndVarargs: Boolean
|
||||
get() = valueParameters.all { !it.hasDefaultValue() && it.varargElementType == null }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun FunctionDescriptor.isValidOperator() = isOperator && OperatorChecks.canBeOperator(this)
|
||||
@@ -1,5 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports any explicit calls of 'get' or 'set' functions which can be replaced by indexing operator []
|
||||
This inspection reports any explicit calls of <code>get</code> or <code>set</code> functions which can be replaced by indexing operator <code>[]</code>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+3
-4
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
import org.jetbrains.kotlin.util.isValidOperator
|
||||
|
||||
public class ReplaceGetOrSetInspection : IntentionBasedInspection<KtDotQualifiedExpression>(
|
||||
ReplaceGetOrSetIntention(), ReplaceGetOrSetInspection.additionalChecker
|
||||
@@ -68,7 +69,7 @@ class ReplaceGetOrSetIntention : JetSelfTargetingRangeIntention<KtDotQualifiedEx
|
||||
if (!resolvedCall.isReallySuccess()) return null
|
||||
|
||||
val target = resolvedCall.resultingDescriptor as? FunctionDescriptor ?: return null
|
||||
if (!target.isOperator || target.name !in operatorNames) return null
|
||||
if (!target.isValidOperator() || target.name !in operatorNames) return null
|
||||
|
||||
if (callExpression.getTypeArgumentList() != null) return null
|
||||
|
||||
@@ -92,9 +93,7 @@ class ReplaceGetOrSetIntention : JetSelfTargetingRangeIntention<KtDotQualifiedEx
|
||||
fun applyTo(element: KtDotQualifiedExpression) {
|
||||
val isSet = element.toResolvedCall(BodyResolveMode.PARTIAL)!!.resultingDescriptor.name == OperatorNameConventions.SET
|
||||
val allArguments = element.callExpression!!.valueArguments
|
||||
if (isSet) {
|
||||
assert(allArguments.size > 0)
|
||||
}
|
||||
assert(allArguments.isNotEmpty())
|
||||
|
||||
val expression = KtPsiFactory(element).buildExpression {
|
||||
appendExpression(element.getReceiverExpression())
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// INTENTION_TEXT: Replace 'set' call with indexing operator
|
||||
|
||||
class C {
|
||||
operator fun set(s: String, value: Int) {}
|
||||
operator fun set(s: String, p: Int, value: Int): Boolean = true
|
||||
}
|
||||
|
||||
class D(val c: C) {
|
||||
fun foo() {
|
||||
this.c.<caret>set("x", 1)
|
||||
this.c.<caret>set("x", 2, 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// INTENTION_TEXT: Replace 'set' call with indexing operator
|
||||
|
||||
class C {
|
||||
operator fun set(s: String, value: Int) {}
|
||||
operator fun set(s: String, p: Int, value: Int): Boolean = true
|
||||
}
|
||||
|
||||
class D(val c: C) {
|
||||
fun foo() {
|
||||
this.c["x"] = 1
|
||||
this.c["x", 2] = 1
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class C {
|
||||
operator fun set(){}
|
||||
}
|
||||
|
||||
class D(val c: C) {
|
||||
fun foo() {
|
||||
this.c.<caret>set()
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class C {
|
||||
operator fun set(s: String, vararg value: Int): Boolean = true
|
||||
}
|
||||
|
||||
class D(val c: C) {
|
||||
fun foo() {
|
||||
this.c.<caret>set("x", 1, 2)
|
||||
}
|
||||
}
|
||||
@@ -2870,6 +2870,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("setWithNoParameters.kt")
|
||||
public void testSetWithNoParameters() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGetOrSet/setWithNoParameters.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("setWithVararg.kt")
|
||||
public void testSetWithVararg() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGetOrSet/setWithVararg.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("singleArgument.kt")
|
||||
public void testSingleArgument() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGetOrSet/singleArgument.kt");
|
||||
|
||||
Reference in New Issue
Block a user