diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveExplicitTypeArguments.kt b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveExplicitTypeArguments.kt index 6e443936ce2..35739c463d7 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/RemoveExplicitTypeArguments.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/RemoveExplicitTypeArguments.kt @@ -31,6 +31,12 @@ import java.util.ArrayList import org.jetbrains.jet.di.InjectorForMacros import org.jetbrains.jet.lang.resolve.BindingTraceContext import org.jetbrains.jet.plugin.caches.resolve.getLazyResolveSession +import org.jetbrains.jet.lang.psi.JetProperty +import org.jetbrains.jet.lang.psi.JetTypeArgumentList +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.psi.JetReturnExpression +import org.jetbrains.jet.lang.psi.JetDeclaration +import org.jetbrains.jet.lang.psi.JetDeclarationWithBody public class RemoveExplicitTypeArguments : JetSelfTargetingIntention( "remove.explicit.type.arguments", javaClass()) { @@ -48,7 +54,23 @@ public class RemoveExplicitTypeArguments : JetSelfTargetingIntention parent.getInitializer() == callExpression && parent.getTypeRef() != null + is JetDeclarationWithBody -> parent.getBodyExpression() == callExpression + is JetReturnExpression -> true + else -> false + } + val jType = if (expectedTypeIsExplicitInCode) { + context[BindingContext.EXPECTED_EXPRESSION_TYPE, callExpression] ?: TypeUtils.NO_EXPECTED_TYPE + } + else { + TypeUtils.NO_EXPECTED_TYPE + } val dataFlow = context[BindingContext.EXPRESSION_DATA_FLOW_INFO, element] ?: DataFlowInfo.EMPTY val resolvedCall = injector.getExpressionTypingServices()?.getCallResolver()?.resolveFunctionCall( BindingTraceContext(), scope, untypedCall, jType, dataFlow, false) diff --git a/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt b/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt new file mode 100644 index 00000000000..4bd0f43732a --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: true + +fun bar(): Foo = foo() + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt.after b/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt.after new file mode 100644 index 00000000000..15bb1a9f0b7 --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt.after @@ -0,0 +1,7 @@ +// IS_APPLICABLE: true + +fun bar(): Foo = foo() + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/expected.xml b/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/expected.xml index 4d264d64d72..72a83dd3487 100644 --- a/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/expected.xml +++ b/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/expected.xml @@ -98,4 +98,30 @@ Remove explicit type arguments + + returnCallWithUnnecessaryTypeArgs.kt + 4 + light_idea_test_case + + Type arguments are unnecessary + Remove explicit type arguments + + + + propertyInitializerIsCallWithUnnecessaryTypeArgs.kt + 4 + light_idea_test_case + + Type arguments are unnecessary + Remove explicit type arguments + + + + functionBodyIsCallWithUnnecessaryTypeArgs.kt + 3 + light_idea_test_case + + Type arguments are unnecessary + Remove explicit type arguments + \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitTypeArguments/nestedCall-KT-5028.kt b/idea/testData/intentions/removeExplicitTypeArguments/nestedCall-KT-5028.kt new file mode 100644 index 00000000000..9559b4861da --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/nestedCall-KT-5028.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false + +class Some +fun foo(c: Some) {} + +fun test() { + foo(Some()) +} \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt b/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt new file mode 100644 index 00000000000..bc2998e1774 --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: true + +fun bar() { + val l: Foo = foo() +} + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt.after b/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt.after new file mode 100644 index 00000000000..9bacdc368a0 --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt.after @@ -0,0 +1,9 @@ +// IS_APPLICABLE: true + +fun bar() { + val l: Foo = foo() +} + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt b/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt new file mode 100644 index 00000000000..80e1612873a --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: true + +fun bar(): Foo { + return foo() +} + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt.after b/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt.after new file mode 100644 index 00000000000..ae5a9060b8c --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt.after @@ -0,0 +1,9 @@ +// IS_APPLICABLE: true + +fun bar(): Foo { + return foo() +} + +class Foo + +fun foo(): Foo = Foo() diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 07cadcde7d0..360dcdb3cb6 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -4092,6 +4092,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/fourLiterals.kt"); } + @TestMetadata("functionBodyIsCallWithUnnecessaryTypeArgs.kt") + public void testFunctionBodyIsCallWithUnnecessaryTypeArgs() throws Exception { + doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/functionBodyIsCallWithUnnecessaryTypeArgs.kt"); + } + @TestMetadata("inapplicableTypeThatIsAFunItCannotBeInferred.kt") public void testInapplicableTypeThatIsAFunItCannotBeInferred() throws Exception { doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/inapplicableTypeThatIsAFunItCannotBeInferred.kt"); @@ -4122,6 +4127,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/literalsWhenTypeArgHasTypeArg.kt"); } + @TestMetadata("nestedCall-KT-5028.kt") + public void testNestedCall_KT_5028() throws Exception { + doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/nestedCall-KT-5028.kt"); + } + @TestMetadata("notApplicableNotEnoughtInfo.kt") public void testNotApplicableNotEnoughtInfo() throws Exception { doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/notApplicableNotEnoughtInfo.kt"); @@ -4137,6 +4147,16 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/notApplicableSupertypeOfInferredClass.kt"); } + @TestMetadata("propertyInitializerIsCallWithUnnecessaryTypeArgs.kt") + public void testPropertyInitializerIsCallWithUnnecessaryTypeArgs() throws Exception { + doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/propertyInitializerIsCallWithUnnecessaryTypeArgs.kt"); + } + + @TestMetadata("returnCallWithUnnecessaryTypeArgs.kt") + public void testReturnCallWithUnnecessaryTypeArgs() throws Exception { + doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/returnCallWithUnnecessaryTypeArgs.kt"); + } + @TestMetadata("twoLiteralValues.kt") public void testTwoLiteralValues() throws Exception { doTestRemoveExplicitTypeArguments("idea/testData/intentions/removeExplicitTypeArguments/twoLiteralValues.kt");