From 7e3d3bde74d292b7785bcf0fc957d80e17dd6b44 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 8 Sep 2017 17:36:16 +0300 Subject: [PATCH] Unused symbol: never use VALUE_ARGUMENT scope for members So #KT-10546 Fixed --- .../org/jetbrains/kotlin/psi/KtPsiUtil.java | 18 +++++++++++++++++- .../unusedSymbol/inAnonymous.kt | 9 +++++++++ .../LocalInspectionTestGenerated.java | 6 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index 1b2a7028bca..bfac7183b5e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -818,9 +818,25 @@ public class KtPsiUtil { return (KtElement) parent; } } - if (current instanceof KtBlockExpression || current instanceof KtParameter || current instanceof KtValueArgument) { + if (current instanceof KtBlockExpression || current instanceof KtParameter) { return (KtElement) current; } + if (current instanceof KtValueArgument) { + // for members, value argument is never enough, see KT-10546 + if (declaration instanceof KtProperty) { + if (((KtProperty) declaration).isLocal()) { + return (KtElement) current; + } + } + else if (declaration instanceof KtFunction) { + if (((KtFunction) declaration).isLocal()) { + return (KtElement) current; + } + } + else { + return (KtElement) current; + } + } if (current instanceof KtDelegatedSuperTypeEntry) { PsiElement grandParent = current.getParent().getParent(); if (grandParent instanceof KtClassOrObject && !(grandParent.getParent() instanceof KtObjectLiteralExpression)) { diff --git a/idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt b/idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt new file mode 100644 index 00000000000..15f71d1bcd7 --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +// WITH_RUNTIME + +fun foo() { + val y = listOf(object { + val value = 0 + })[0] + y.value +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index db8746ff195..45a15659500 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1850,6 +1850,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unusedSymbol"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("inAnonymous.kt") + public void testInAnonymous() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedSymbol/inAnonymous.kt"); + doTest(fileName); + } + @TestMetadata("internal.kt") public void testInternal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedSymbol/internal.kt");