For each parameter unused: support correctly it() case

Related to KT-22068
This commit is contained in:
Mikhail Glukhikh
2018-08-09 12:53:09 +03:00
parent 0757533558
commit 6048647812
4 changed files with 36 additions and 4 deletions
@@ -11,12 +11,13 @@ import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl.WithDestructuringDeclaration
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
import org.jetbrains.kotlin.idea.refactoring.getThisLabelName
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
class ForEachParameterNotUsedInspection : AbstractKotlinInspection() {
@@ -59,10 +60,12 @@ class ForEachParameterNotUsedInspection : AbstractKotlinInspection() {
if (element.children.isNotEmpty()) {
element.acceptChildren(this)
} else {
val bindingContext = element.analyze()
val resolvedCall = element.getResolvedCall(bindingContext) ?: return
val resolvedCall = element.resolveToCall() ?: return
used = resolvedCall.candidateDescriptor == descriptor
used = descriptor == when (resolvedCall) {
is VariableAsFunctionResolvedCall -> resolvedCall.variableCall.candidateDescriptor
else -> resolvedCall.candidateDescriptor
}
}
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ForEachParameterNotUsedInspection
@@ -0,0 +1,10 @@
// PROBLEM: none
// WITH_RUNTIME
class My {
operator fun invoke() {}
}
fun bar(my: List<My>) {
my.for<caret>Each { it() }
}
@@ -2161,6 +2161,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/forEachParameterNotUsed")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ForEachParameterNotUsed extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInForEachParameterNotUsed() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/forEachParameterNotUsed"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("invoke.kt")
public void testInvoke() throws Exception {
runTest("idea/testData/inspectionsLocal/forEachParameterNotUsed/invoke.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/ImplicitNullableNothingType")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)