diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index e8324ec064c..ff2d408d836 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -26,6 +26,7 @@ import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; @@ -131,4 +132,22 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction } }; } + + @NotNull + public static JetIntentionActionFactory createFactoryForCompareToTypeMismatch() { + return new JetIntentionActionFactory() { + @Nullable + @Override + public IntentionAction createAction(Diagnostic diagnostic) { + JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); + assert expression != null : "COMPARE_TO_TYPE_MISMATCH reported on element that is not within any expression"; + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) expression.getContainingFile()).getBindingContext(); + ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); + if (resolvedCall == null) return null; + PsiElement compareTo = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + if (!(compareTo instanceof JetFunction)) return null; + return new ChangeFunctionReturnTypeFix((JetFunction) compareTo, KotlinBuiltIns.getInstance().getIntType()); + } + }; + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 18e70b0dc71..3bd2db28197 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -214,6 +214,7 @@ public class QuickFixes { factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, ChangePropertyTypeToMatchOverriddenPropertyFix.createFactory()); factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForComponentFunctionReturnTypeMismatch()); factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch()); + factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch()); factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory()); } diff --git a/idea/testData/quickfix/typeMismatch/afterCompareToTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/afterCompareToTypeMismatch.kt new file mode 100644 index 00000000000..218c5517070 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterCompareToTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'A.compareTo' function return type to 'Int'" "true" +trait A { + fun compareTo(other: Any): Int +} +fun foo(x: A) { + if (x < 0) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt new file mode 100644 index 00000000000..1033e051114 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'A.compareTo' function return type to 'Int'" "true" +trait A { + fun compareTo(other: Any): String +} +fun foo(x: A) { + if (x < 0) {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index f86538fe78c..28202b3490e 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -977,6 +977,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { public void testChangeReturnTypeWhenValueParameterListIsAbsent() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenValueParameterListIsAbsent.kt"); } + + @TestMetadata("beforeCompareToTypeMismatch.kt") + public void testCompareToTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt"); + } @TestMetadata("beforeComponentFunctionReturnTypeMismatch1.kt") public void testComponentFunctionReturnTypeMismatch1() throws Exception {