From b0e2afc8cba8e206efc987414dc4e2e1d2381332 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Mon, 19 Jan 2015 16:25:05 +0300 Subject: [PATCH] Searching for property accessors, too. --- .../idea/inspections/UnusedSymbolInspection.kt | 17 ++++++++++++----- .../unusedSymbol/property/usedInJava.java | 12 ++++++++++++ .../unusedSymbol/property/usedInJava.kt | 8 ++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 idea/testData/inspections/unusedSymbol/property/usedInJava.java create mode 100644 idea/testData/inspections/unusedSymbol/property/usedInJava.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 4baba1006ed..a3ae212e20e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.INVOKE_OPERATION_NAME import org.jetbrains.kotlin.psi.JetEnumEntry import org.jetbrains.kotlin.psi.JetProperty import org.jetbrains.kotlin.idea.search.usagesSearch.PropertyUsagesSearchHelper +import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames public class UnusedSymbolInspection : AbstractKotlinInspection() { private val javaInspection = UnusedDeclarationInspection() @@ -137,12 +138,18 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() { val useScope = declaration.getUseScope() if (useScope is GlobalSearchScope) { - val searchCostResult = psiSearchHelper.isCheapEnoughToSearch(declaration.getName(), useScope, null, null) + var zeroOccurrences = true - when (searchCostResult) { - ZERO_OCCURRENCES -> return false // function is surely unused - FEW_OCCURRENCES -> {} // do search (following code) - TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used + for (name in listOf(declaration.getName()) + declaration.getAccessorNames()) { + when (psiSearchHelper.isCheapEnoughToSearch(name, useScope, null, null)) { + ZERO_OCCURRENCES -> {} // go on, check other names + FEW_OCCURRENCES -> zeroOccurrences = false + TOO_MANY_OCCURRENCES -> return true // searching usages is too expensive; behave like it is used + } + } + + if (zeroOccurrences) { + return false } } diff --git a/idea/testData/inspections/unusedSymbol/property/usedInJava.java b/idea/testData/inspections/unusedSymbol/property/usedInJava.java new file mode 100644 index 00000000000..466bfa269e8 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/property/usedInJava.java @@ -0,0 +1,12 @@ +package test; + +import foo.FooPackage; +import foo.Obj; + +class usedInJava { + public static void main(String[] args) { + FooPackage.getUsedByGetter(); + FooPackage.setUsedBySetter(":|"); + System.out.println(Obj.CONST); + } +} \ No newline at end of file diff --git a/idea/testData/inspections/unusedSymbol/property/usedInJava.kt b/idea/testData/inspections/unusedSymbol/property/usedInJava.kt new file mode 100644 index 00000000000..883bd3660a1 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/property/usedInJava.kt @@ -0,0 +1,8 @@ +package foo + +val usedByGetter = ":)" +var usedBySetter = ":)" + +object Obj { + val CONST = ":)" +} \ No newline at end of file