"Receiver parameter is never used" inspection: don't report it when anonymous function is called with receiver

#KT-41246 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-10-02 11:07:25 +09:00
committed by Dmitry Gridin
parent 112e07814e
commit bcbb90dd81
8 changed files with 67 additions and 2 deletions
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInsight.daemon.impl.HighlightInfoType
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
@@ -18,6 +17,7 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.isOverridable
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.isMainFunction
import org.jetbrains.kotlin.idea.quickfix.RemoveUnusedFunctionParameterFix
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
import org.jetbrains.kotlin.psi.typeRefHelpers.setReceiverTypeReference
import org.jetbrains.kotlin.resolve.BindingContext
@@ -49,7 +50,13 @@ class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
if (receiverTypeReference == null || receiverTypeReference.textRange.isEmpty) return
if (callableDeclaration is KtProperty && callableDeclaration.accessors.isEmpty()) return
if (callableDeclaration is KtNamedFunction && !callableDeclaration.hasBody()) return
if (callableDeclaration is KtNamedFunction) {
if (!callableDeclaration.hasBody()) return
if (callableDeclaration.name == null) {
val parentQualified = callableDeclaration.getStrictParentOfType<KtQualifiedExpression>()
if (KtPsiUtil.deparenthesize(parentQualified?.callExpression?.calleeExpression) == callableDeclaration) return
}
}
if (callableDeclaration.hasModifier(KtTokens.OVERRIDE_KEYWORD) ||
callableDeclaration.hasModifier(KtTokens.OPERATOR_KEYWORD) ||
@@ -0,0 +1,6 @@
class A {
fun a() {
val f = fun <caret>A.() {
}
}
}
@@ -0,0 +1,6 @@
class A {
fun a() {
val f = fun() {
}
}
}
@@ -0,0 +1,6 @@
class A {
fun a() {
(fun <caret>A.() {
})()
}
}
@@ -0,0 +1,6 @@
class A {
fun a() {
(fun() {
})()
}
}
@@ -0,0 +1,7 @@
// PROBLEM: none
class A {
fun a() {
this.(fun <caret>A.() {
})()
}
}
@@ -0,0 +1,7 @@
// PROBLEM: none
class A
fun test(a: A) {
a.(fun <caret>A.() {
})()
}
@@ -14250,6 +14250,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/unusedReceiverParameter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
}
@TestMetadata("anonymousFunction.kt")
public void testAnonymousFunction() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedReceiverParameter/anonymousFunction.kt");
}
@TestMetadata("anonymousFunctionCall.kt")
public void testAnonymousFunctionCall() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedReceiverParameter/anonymousFunctionCall.kt");
}
@TestMetadata("anonymousFunctionCallWithReceiver.kt")
public void testAnonymousFunctionCallWithReceiver() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedReceiverParameter/anonymousFunctionCallWithReceiver.kt");
}
@TestMetadata("anonymousFunctionCallWithReceiver2.kt")
public void testAnonymousFunctionCallWithReceiver2() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedReceiverParameter/anonymousFunctionCallWithReceiver2.kt");
}
@TestMetadata("companion.kt")
public void testCompanion() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedReceiverParameter/companion.kt");