diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedReceiverParameterInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedReceiverParameterInspection.kt index e3699e54adf..9e58c961e2d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedReceiverParameterInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedReceiverParameterInspection.kt @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.idea.util.getThisReceiverOwner import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall @@ -48,7 +49,11 @@ class UnusedReceiverParameterInspection : AbstractKotlinInspection() { private fun check(callableDeclaration: KtCallableDeclaration) { val receiverTypeReference = callableDeclaration.receiverTypeReference if (receiverTypeReference == null || receiverTypeReference.textRange.isEmpty) return - if (callableDeclaration.isOverridable() || callableDeclaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return + + if (callableDeclaration.isOverridable() || + callableDeclaration.hasModifier(KtTokens.OVERRIDE_KEYWORD) || + callableDeclaration.hasModifier(KtTokens.OPERATOR_KEYWORD) || + callableDeclaration.hasModifier(KtTokens.INFIX_KEYWORD)) return if (callableDeclaration is KtProperty && callableDeclaration.accessors.isEmpty()) return if (callableDeclaration is KtNamedFunction && !callableDeclaration.hasBody()) return diff --git a/idea/testData/inspectionsLocal/unusedReceiverParameter/.inspection b/idea/testData/inspectionsLocal/unusedReceiverParameter/.inspection new file mode 100644 index 00000000000..d2d61d0897d --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedReceiverParameter/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.UnusedReceiverParameterInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/unusedReceiverParameter/companion.kt b/idea/testData/inspectionsLocal/unusedReceiverParameter/companion.kt new file mode 100644 index 00000000000..05165335ad5 --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedReceiverParameter/companion.kt @@ -0,0 +1,16 @@ +// PROBLEM: none + +class Test(val test: Int) { + companion object + + fun foo() = test +} + +// Used +operator fun Test.Companion.invoke() = Test(1) + + +fun main(args: Array) { + val x = Test() + x.foo() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/unusedReceiverParameter/infix.kt b/idea/testData/inspectionsLocal/unusedReceiverParameter/infix.kt new file mode 100644 index 00000000000..1f0afc98543 --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedReceiverParameter/infix.kt @@ -0,0 +1,14 @@ +// PROBLEM: none + +class Test(val test: Int) { + fun foo() = test +} + +// Receiver unused but still inapplicable (infix) +infix fun Test.build(x: Int) = Test(x * x) + + +fun main(args: Array) { + val x = Test(0) build 7 + x.foo() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/unusedReceiverParameter/operator.kt b/idea/testData/inspectionsLocal/unusedReceiverParameter/operator.kt new file mode 100644 index 00000000000..a903ebd6c98 --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedReceiverParameter/operator.kt @@ -0,0 +1,14 @@ +// PROBLEM: none + +class Test(val test: Int) { + fun foo() = test +} + +// Receiver unused but still inapplicable (operator!) +operator fun Test.invoke(x: Int, y: Int) = Test(x + y) + + +fun main(args: Array) { + val x = Test(0)(1, 2) + x.foo() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 24492f02466..7e11071b58c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1920,6 +1920,33 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class UnusedReceiverParameter extends AbstractLocalInspectionTest { + public void testAllFilesPresentInUnusedReceiverParameter() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unusedReceiverParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("companion.kt") + public void testCompanion() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter/companion.kt"); + doTest(fileName); + } + + @TestMetadata("infix.kt") + public void testInfix() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter/infix.kt"); + doTest(fileName); + } + + @TestMetadata("operator.kt") + public void testOperator() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter/operator.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/inspectionsLocal/unusedSymbol") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)