KT-7525 Don't suggest to replace 'get' call with index operator for static method calls (inspection & intention)

KT-5322 super.get issues a "Replace 'get' call with index operator" inspection

 #KT-7525 Fixed
 #KT-5322 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-05-14 14:45:26 +03:00
parent 0fe334fd55
commit 4c88b31878
22 changed files with 230 additions and 9 deletions
@@ -121,3 +121,10 @@ fun JetExpression.isExitStatement(): Boolean {
}
}
// returns false for call of super, static method or method from package
fun JetQualifiedExpression.isReceiverExpressionWithValue(): Boolean {
val receiver = getReceiverExpression()
if (receiver is JetSuperExpression) return false
return analyze().getType(receiver) != null
}
@@ -18,10 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
import org.jetbrains.kotlin.idea.intentions.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
@@ -32,11 +29,15 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class ReplaceCallWithBinaryOperatorIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with binary operator") {
override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
val operation = operation(element.calleeName) ?: return null
val resolvedCall = element.toResolvedCall() ?: return null
if (!resolvedCall.getStatus().isSuccess()) return null
if (resolvedCall.getCall().getTypeArgumentList() != null) return null
val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null
if ((resolvedCall.getArgumentMapping(argument) as ArgumentMatch).valueParameter.getIndex() != 0) return null
if (!element.isReceiverExpressionWithValue()) return null
setText("Replace with '$operation' operator")
return element.callExpression!!.getCalleeExpression()!!.getTextRange()
}
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.idea.intentions.isReceiverExpressionWithValue
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
@@ -30,9 +31,13 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class ReplaceCallWithUnaryOperatorIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace call with unary operator") {
override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
val operation = operation(element.calleeName) ?: return null
val call = element.callExpression ?: return null
if (call.getTypeArgumentList() != null) return null
if (!call.getValueArguments().isEmpty()) return null
if (!element.isReceiverExpressionWithValue()) return null
setText("Replace with '$operation' operator")
return call.getCalleeExpression()!!.getTextRange()
}
@@ -18,10 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
import org.jetbrains.kotlin.idea.intentions.*
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
@@ -31,6 +28,7 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class ReplaceContainsIntention : JetSelfTargetingRangeIntention<JetDotQualifiedExpression>(javaClass(), "Replace 'contains' call with 'in' operator") {
override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? {
if (element.calleeName != OperatorConventions.CONTAINS.asString()) return null
val resolvedCall = element.toResolvedCall() ?: return null
if (!resolvedCall.getStatus().isSuccess()) return null
val argument = resolvedCall.getCall().getValueArguments().singleOrNull() ?: return null
@@ -39,6 +37,9 @@ public class ReplaceContainsIntention : JetSelfTargetingRangeIntention<JetDotQua
val target = resolvedCall.getResultingDescriptor()
val returnType = target.getReturnType() ?: return null
if (!target.builtIns.isBooleanOrSubtype(returnType)) return null
if (!element.isReceiverExpressionWithValue()) return null
return element.callExpression!!.getCalleeExpression()!!.getTextRange()
}
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.idea.intentions.isReceiverExpressionWithValue
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.buildExpression
@@ -33,9 +34,13 @@ public class ReplaceGetIntention : JetSelfTargetingRangeIntention<JetDotQualifie
if (element.calleeName != "get") return null
val call = element.callExpression ?: return null
if (call.getTypeArgumentList() != null) return null
val arguments = call.getValueArguments()
if (arguments.isEmpty()) return null
if (arguments.any { it.isNamed() }) return null
if (!element.isReceiverExpressionWithValue()) return null
return call.getCalleeExpression()!!.getTextRange()
}