Unused symbol: fix false positive in anonymous object in top level or companion object

#KT-31800 Fixed
#KT-20868 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-02-10 13:38:16 +09:00
committed by Vladimir Dolzhenko
parent ef1e54eda9
commit a3252b9480
10 changed files with 106 additions and 0 deletions
@@ -130,6 +130,21 @@ abstract class KtNamedDeclarationStub<T extends KotlinStubWithFqName<?>> extends
KtElement enclosingBlock = KtPsiUtil.getEnclosingElementForLocalDeclaration(this, false);
if (enclosingBlock != null) {
PsiElement enclosingParent = enclosingBlock.getParent();
KtObjectDeclaration containingObject = PsiTreeUtil.getParentOfType(this, KtObjectDeclaration.class);
if (containingObject != null && containingObject.isObjectLiteral()) {
KtDeclarationWithInitializer declaration =
PsiTreeUtil.getParentOfType(containingObject, KtDeclarationWithInitializer.class);
if (declaration != null && declaration.hasModifier(KtTokens.PRIVATE_KEYWORD)) {
if (enclosingParent instanceof KtFile) {
return new LocalSearchScope(enclosingParent);
}
if (enclosingParent instanceof KtObjectDeclaration && ((KtObjectDeclaration) enclosingParent).isCompanion()) {
return new LocalSearchScope(enclosingParent.getParent());
}
}
}
if (enclosingParent instanceof KtContainerNode) {
enclosingParent = enclosingParent.getParent();
}
@@ -0,0 +1,14 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtNamedFunction
// OPTIONS: usages
class Foo {
companion object {
private val localObject = object : Any() {
fun <caret>f() {
}
}
}
fun bar() {
localObject.f()
}
}
@@ -0,0 +1 @@
Function call 12 localObject.f()
@@ -0,0 +1,12 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtNamedFunction
// OPTIONS: usages
private val localObject = object : Any() {
fun <caret>f() {
}
}
class Foo {
fun bar() {
localObject.f()
}
}
@@ -0,0 +1 @@
Function call 10 localObject.f()
@@ -0,0 +1,16 @@
// PROBLEM: none
// WITH_RUNTIME
private val xs = listOf(1, 2, 3).flatMap { x ->
listOf(3, 4, 5).map { y ->
object {
val <caret>value = x + y
}
}
}
fun foo() {
xs.forEach {
println("value: " + it.value)
}
}
@@ -0,0 +1,13 @@
// PROBLEM: none
class Some {
fun bar() {
writer().sayHello()
}
companion object {
private fun writer() = object {
fun <caret>sayHello() {
}
}
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
fun main() {
writer.sayHello()
}
private val writer = object {
fun <caret>sayHello() {
}
}
@@ -719,6 +719,16 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMemberInClass.0.kt");
}
@TestMetadata("objectExpressionMemberInCompanion.0.kt")
public void testObjectExpressionMemberInCompanion() throws Exception {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMemberInCompanion.0.kt");
}
@TestMetadata("objectExpressionMemberInTopLevel.0.kt")
public void testObjectExpressionMemberInTopLevel() throws Exception {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMemberInTopLevel.0.kt");
}
@TestMetadata("synthesizedFunction.0.kt")
public void testSynthesizedFunction() throws Exception {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/synthesizedFunction.0.kt");
@@ -13019,6 +13019,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/unusedSymbol/inAnonymousDeeply.kt");
}
@TestMetadata("inAnonymousDeeplyInTopLevel.kt")
public void testInAnonymousDeeplyInTopLevel() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedSymbol/inAnonymousDeeplyInTopLevel.kt");
}
@TestMetadata("inAnonymousInCompanion.kt")
public void testInAnonymousInCompanion() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedSymbol/inAnonymousInCompanion.kt");
}
@TestMetadata("inAnonymousInTopLovel.kt")
public void testInAnonymousInTopLovel() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedSymbol/inAnonymousInTopLovel.kt");
}
@TestMetadata("inAnonymousRunWrapped.kt")
public void testInAnonymousRunWrapped() throws Exception {
runTest("idea/testData/inspectionsLocal/unusedSymbol/inAnonymousRunWrapped.kt");