Can be replaced with binary operator: do not suggest when receiver or argument is floating point type
#KT-28596 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
48619a49ed
commit
d583c1be58
+4
-3
@@ -25,7 +25,8 @@ fun KtExpression?.getKotlinTypeWithPossibleSmartCastToFP(
|
||||
bindingContext: BindingContext,
|
||||
descriptor: DeclarationDescriptor?,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
dataFlowValueFactory: DataFlowValueFactory
|
||||
dataFlowValueFactory: DataFlowValueFactory,
|
||||
defaultType: (KotlinType, Set<KotlinType>) -> KotlinType = { givenType, _ -> givenType }
|
||||
): KotlinType? {
|
||||
val givenType = this?.getKotlinTypeForComparison(bindingContext) ?: return null
|
||||
|
||||
@@ -40,13 +41,13 @@ fun KtExpression?.getKotlinTypeWithPossibleSmartCastToFP(
|
||||
if (descriptor != null) {
|
||||
val dataFlow = dataFlowValueFactory.createDataFlowValue(this, givenType, bindingContext, descriptor)
|
||||
val stableTypes = bindingContext.getDataFlowInfoBefore(this).getStableTypes(dataFlow, languageVersionSettings)
|
||||
stableTypes.firstNotNullResult {
|
||||
return stableTypes.firstNotNullResult {
|
||||
when {
|
||||
KotlinBuiltIns.isDoubleOrNullableDouble(it) -> it
|
||||
KotlinBuiltIns.isFloatOrNullableFloat(it) -> it
|
||||
else -> null
|
||||
}
|
||||
}?.let { return it }
|
||||
} ?: defaultType(givenType, stableTypes)
|
||||
}
|
||||
return givenType
|
||||
}
|
||||
+4
-2
@@ -187,12 +187,14 @@ class ReplaceCallWithBinaryOperatorInspection : AbstractApplicabilityBasedInspec
|
||||
private fun KtDotQualifiedExpression.isFloatingPointNumberEquals(): Boolean {
|
||||
val resolvedCall = resolveToCall() ?: return false
|
||||
val context = analyze(BodyResolveMode.PARTIAL)
|
||||
val declarationDescriptor = containingDeclarationForPseudocode?.resolveToDescriptorIfAny()
|
||||
val dataFlowValueFactory = getResolutionFacade().getFrontendService(DataFlowValueFactory::class.java)
|
||||
val defaultType: (KotlinType, Set<KotlinType>) -> KotlinType = { givenType, stableTypes -> stableTypes.firstOrNull() ?: givenType }
|
||||
val receiverType = resolvedCall.getReceiverExpression()?.getKotlinTypeWithPossibleSmartCastToFP(
|
||||
context, containingDeclarationForPseudocode?.resolveToDescriptorIfAny(), languageVersionSettings, dataFlowValueFactory
|
||||
context, declarationDescriptor, languageVersionSettings, dataFlowValueFactory, defaultType
|
||||
) ?: return false
|
||||
val argumentType = resolvedCall.getFirstArgumentExpression()?.getKotlinTypeWithPossibleSmartCastToFP(
|
||||
context, containingDeclarationForPseudocode?.resolveToDescriptorIfAny(), languageVersionSettings, dataFlowValueFactory
|
||||
context, declarationDescriptor, languageVersionSettings, dataFlowValueFactory, defaultType
|
||||
) ?: return false
|
||||
return receiverType.isFpType() && argumentType.isNumericType() ||
|
||||
argumentType.isFpType() && receiverType.isNumericType()
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// FIX: Replace total order equality with IEEE 754 equality
|
||||
|
||||
fun test(a: Any, b: Any) =
|
||||
a is Double && b is Int && a.<caret>equals(b)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// FIX: Replace total order equality with IEEE 754 equality
|
||||
|
||||
fun test(a: Any, b: Any) =
|
||||
a is Double && b is Int && a == b
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// FIX: Replace total order equality with IEEE 754 equality
|
||||
|
||||
fun test(a: Any, b: Any) =
|
||||
a is Int && b is Double && a.<caret>equals(b)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// FIX: Replace total order equality with IEEE 754 equality
|
||||
|
||||
fun test(a: Any, b: Any) =
|
||||
a is Int && b is Double && a == b
|
||||
+10
@@ -1679,6 +1679,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceCallWithBinaryOperator/equalsDoubleSmartCast.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("equalsDoubleSmartCast2.kt")
|
||||
public void testEqualsDoubleSmartCast2() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceCallWithBinaryOperator/equalsDoubleSmartCast2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("equalsDoubleSmartCast3.kt")
|
||||
public void testEqualsDoubleSmartCast3() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceCallWithBinaryOperator/equalsDoubleSmartCast3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("equalsDoubleSmartCastFromGeneric.kt")
|
||||
public void testEqualsDoubleSmartCastFromGeneric() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/conventionNameCalls/replaceCallWithBinaryOperator/equalsDoubleSmartCastFromGeneric.kt");
|
||||
|
||||
Reference in New Issue
Block a user