KT-7472 function of anonymous class is never used
#KT-7472 fixed
This commit is contained in:
@@ -793,6 +793,14 @@ public class JetPsiUtil {
|
||||
return getEnclosingElementForLocalDeclaration(declaration, true);
|
||||
}
|
||||
|
||||
private static boolean isMemberOfObjectExpression(@NotNull JetCallableDeclaration propertyOrFunction) {
|
||||
PsiElement parent = PsiTreeUtil.getStubOrPsiParent(propertyOrFunction);
|
||||
if (!(parent instanceof JetClassBody)) return false;
|
||||
PsiElement grandparent = PsiTreeUtil.getStubOrPsiParent(parent);
|
||||
if (!(grandparent instanceof JetObjectDeclaration)) return false;
|
||||
return PsiTreeUtil.getStubOrPsiParent(grandparent) instanceof JetObjectLiteralExpression;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static JetElement getEnclosingElementForLocalDeclaration(@NotNull JetDeclaration declaration, boolean skipParameters) {
|
||||
if (declaration instanceof JetTypeParameter && skipParameters) {
|
||||
@@ -819,10 +827,12 @@ public class JetPsiUtil {
|
||||
if (current instanceof JetClassInitializer) {
|
||||
return ((JetClassInitializer) current).getBody();
|
||||
}
|
||||
if (current instanceof JetBlockExpression ||
|
||||
current instanceof JetProperty ||
|
||||
current instanceof JetParameter ||
|
||||
current instanceof JetFunction) {
|
||||
if (current instanceof JetProperty || current instanceof JetFunction) {
|
||||
if (parent instanceof JetFile || (parent instanceof JetClassBody && !isMemberOfObjectExpression((JetCallableDeclaration) current))) {
|
||||
return (JetElement) current;
|
||||
}
|
||||
}
|
||||
if (current instanceof JetBlockExpression || current instanceof JetParameter) {
|
||||
return (JetElement) current;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// PSI_ELEMENT: org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
// OPTIONS: usages
|
||||
package anonymousUnused
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
class LocalClass {
|
||||
fun <caret>f() {
|
||||
}
|
||||
}
|
||||
|
||||
LocalClass().f()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Function call (11: 18) LocalClass().f()
|
||||
@@ -0,0 +1,15 @@
|
||||
// PSI_ELEMENT: org.jetbrains.kotlin.psi.JetProperty
|
||||
// OPTIONS: usages
|
||||
package anonymousUnused
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = object {
|
||||
val b = object {
|
||||
val c = object {
|
||||
val <caret>d = 5
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a.b.c.d
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
Value read (14: 11) a.b.c.d
|
||||
@@ -0,0 +1,12 @@
|
||||
// PSI_ELEMENT: org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
// OPTIONS: usages
|
||||
package anonymousUnused
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val localObject = object : Any() {
|
||||
fun <caret>f() {
|
||||
}
|
||||
}
|
||||
|
||||
localObject.f()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Function call (11: 17) localObject.f()
|
||||
@@ -0,0 +1,12 @@
|
||||
// PSI_ELEMENT: org.jetbrains.kotlin.psi.JetNamedFunction
|
||||
// OPTIONS: usages
|
||||
package anonymousUnused
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
fun localObject() = object : Any() {
|
||||
fun <caret>f() {
|
||||
}
|
||||
}
|
||||
|
||||
localObject().f()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Function call (11: 19) localObject().f()
|
||||
@@ -0,0 +1,45 @@
|
||||
fun main(args: Array<String>) {
|
||||
val localObject = object {
|
||||
fun f() {
|
||||
}
|
||||
|
||||
val p = 5
|
||||
}
|
||||
|
||||
localObject.f()
|
||||
localObject.p
|
||||
|
||||
|
||||
fun localObject2() = object {
|
||||
fun f() {
|
||||
}
|
||||
|
||||
val p = 5
|
||||
}
|
||||
|
||||
localObject2().f()
|
||||
localObject2().p
|
||||
|
||||
|
||||
val a = object {
|
||||
val b = object {
|
||||
val c = object {
|
||||
val d = 5
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a.b.c.d
|
||||
|
||||
|
||||
class LocalClass {
|
||||
fun f() {
|
||||
}
|
||||
|
||||
val p = 5
|
||||
}
|
||||
|
||||
|
||||
LocalClass().f()
|
||||
LocalClass().p
|
||||
}
|
||||
@@ -618,6 +618,30 @@ public class JetFindUsagesTestGenerated extends AbstractJetFindUsagesTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("localClassMember.0.kt")
|
||||
public void testLocalClassMember() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/localClassMember.0.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionDeepMember.0.kt")
|
||||
public void testObjectExpressionDeepMember() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionDeepMember.0.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionMember.0.kt")
|
||||
public void testObjectExpressionMember() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMember.0.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectExpressionMember2.0.kt")
|
||||
public void testObjectExpressionMember2() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMember2.0.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("synthesizedFunction.0.kt")
|
||||
public void testSynthesizedFunction() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findFunctionUsages/synthesizedFunction.0.kt");
|
||||
|
||||
Reference in New Issue
Block a user