Do not report "unused receiver" on operator and infix functions

So #KT-16340 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-10-23 19:13:46 +03:00
parent 54990e16c1
commit 149daa173c
6 changed files with 78 additions and 1 deletions
@@ -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
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.UnusedReceiverParameterInspection
@@ -0,0 +1,16 @@
// PROBLEM: none
class Test(val test: Int) {
companion object
fun foo() = test
}
// Used
operator fun Test.<caret>Companion.invoke() = Test(1)
fun main(args: Array<String>) {
val x = Test()
x.foo()
}
@@ -0,0 +1,14 @@
// PROBLEM: none
class Test(val test: Int) {
fun foo() = test
}
// Receiver unused but still inapplicable (infix)
infix fun <caret>Test.build(x: Int) = Test(x * x)
fun main(args: Array<String>) {
val x = Test(0) build 7
x.foo()
}
@@ -0,0 +1,14 @@
// PROBLEM: none
class Test(val test: Int) {
fun foo() = test
}
// Receiver unused but still inapplicable (operator!)
operator fun <caret>Test.invoke(x: Int, y: Int) = Test(x + y)
fun main(args: Array<String>) {
val x = Test(0)(1, 2)
x.foo()
}
@@ -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)