Do not suggest "redundant toInt() call" for comparison receiver
So #KT-23133 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
f91819ace7
commit
488e5e9f60
+28
-17
@@ -26,24 +26,30 @@ import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
|||||||
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
|
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||||
|
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||||
import org.jetbrains.kotlin.types.isFlexible
|
import org.jetbrains.kotlin.types.isFlexible
|
||||||
|
|
||||||
class RemoveRedundantCallsOfConversionMethodsInspection : IntentionBasedInspection<KtQualifiedExpression>(
|
class RemoveRedundantCallsOfConversionMethodsInspection : IntentionBasedInspection<KtQualifiedExpression>(
|
||||||
RemoveRedundantCallsOfConversionMethodsIntention::class
|
RemoveRedundantCallsOfConversionMethodsIntention::class
|
||||||
) {
|
) {
|
||||||
override fun problemHighlightType(element: KtQualifiedExpression) = ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
override fun problemHighlightType(element: KtQualifiedExpression) = ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||||
}
|
}
|
||||||
|
|
||||||
class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeIntention<KtQualifiedExpression>(KtQualifiedExpression::class.java, "Remove redundant calls of the conversion method") {
|
class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeIntention<KtQualifiedExpression>(
|
||||||
|
KtQualifiedExpression::class.java,
|
||||||
|
"Remove redundant calls of the conversion method"
|
||||||
|
) {
|
||||||
|
|
||||||
private val targetClassMap = mapOf("toString()" to String::class.qualifiedName,
|
private val targetClassMap = mapOf(
|
||||||
"toDouble()" to Double::class.qualifiedName,
|
"toString()" to String::class.qualifiedName,
|
||||||
"toFloat()" to Float::class.qualifiedName,
|
"toDouble()" to Double::class.qualifiedName,
|
||||||
"toLong()" to Long::class.qualifiedName,
|
"toFloat()" to Float::class.qualifiedName,
|
||||||
"toInt()" to Int::class.qualifiedName,
|
"toLong()" to Long::class.qualifiedName,
|
||||||
"toChar()" to Char::class.qualifiedName,
|
"toInt()" to Int::class.qualifiedName,
|
||||||
"toShort()" to Short::class.qualifiedName,
|
"toChar()" to Char::class.qualifiedName,
|
||||||
"toByte()" to Byte::class.qualifiedName)
|
"toShort()" to Short::class.qualifiedName,
|
||||||
|
"toByte()" to Byte::class.qualifiedName
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
override fun applyTo(element: KtQualifiedExpression, editor: Editor?) {
|
override fun applyTo(element: KtQualifiedExpression, editor: Editor?) {
|
||||||
@@ -54,10 +60,9 @@ class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeInten
|
|||||||
val selectorExpression = element.selectorExpression ?: return null
|
val selectorExpression = element.selectorExpression ?: return null
|
||||||
val selectorExpressionText = selectorExpression.text
|
val selectorExpressionText = selectorExpression.text
|
||||||
val qualifiedName = targetClassMap[selectorExpressionText] ?: return null
|
val qualifiedName = targetClassMap[selectorExpressionText] ?: return null
|
||||||
return if(element.receiverExpression.isApplicableReceiverExpression(qualifiedName)) {
|
return if (element.receiverExpression.isApplicableReceiverExpression(qualifiedName)) {
|
||||||
selectorExpression.textRange
|
selectorExpression.textRange
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,10 +72,16 @@ class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeInten
|
|||||||
is KtStringTemplateExpression -> String::class.qualifiedName
|
is KtStringTemplateExpression -> String::class.qualifiedName
|
||||||
is KtConstantExpression -> getType(analyze())?.getJetTypeFqName(false)
|
is KtConstantExpression -> getType(analyze())?.getJetTypeFqName(false)
|
||||||
else -> {
|
else -> {
|
||||||
resolveToCall()?.candidateDescriptor?.returnType?.let {
|
val resolvedCall = resolveToCall()
|
||||||
if (it.isFlexible()) null
|
if ((resolvedCall?.call?.callElement as? KtBinaryExpression)?.operationToken in OperatorConventions.COMPARISON_OPERATIONS) {
|
||||||
else if (it.isMarkedNullable && parent !is KtSafeQualifiedExpression) null
|
// Special case here because compareTo returns Int
|
||||||
else it.getJetTypeFqName(false)
|
Boolean::class.qualifiedName
|
||||||
|
} else {
|
||||||
|
resolvedCall?.candidateDescriptor?.returnType?.let {
|
||||||
|
if (it.isFlexible()) null
|
||||||
|
else if (it.isMarkedNullable && parent !is KtSafeQualifiedExpression) null
|
||||||
|
else it.getJetTypeFqName(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} == qualifiedName
|
} == qualifiedName
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun Boolean.toInt() = if (this) 1 else 0
|
||||||
|
|
||||||
|
fun test(x: Int, y: Int): Int {
|
||||||
|
return (x > y).toInt()<caret>
|
||||||
|
}
|
||||||
@@ -14010,6 +14010,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeRedundantCallsOfConversionMethods"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeRedundantCallsOfConversionMethods"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("booleanToInt.kt")
|
||||||
|
public void testBooleanToInt() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods/booleanToInt.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("byte.kt")
|
@TestMetadata("byte.kt")
|
||||||
public void testByte() throws Exception {
|
public void testByte() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods/byte.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods/byte.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user