diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 4bbc03b537c..2c62661b254 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -39,6 +39,8 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchHelper import org.jetbrains.kotlin.idea.search.usagesSearch.ClassUsagesSearchHelper import org.jetbrains.kotlin.asJava.toLightClass import org.jetbrains.kotlin.idea.search.usagesSearch.FunctionUsagesSearchHelper +import com.intellij.psi.search.PsiSearchHelper +import com.intellij.psi.search.PsiSearchHelper.SearchCostResult.* public class UnusedSymbolInspection : AbstractKotlinInspection() { private val javaInspection = UnusedDeclarationInspection() @@ -104,13 +106,25 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() { } private fun hasNonTrivialUsages(declaration: JetNamedDeclaration): Boolean { + val psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(declaration.getProject()) + + val useScope = declaration.getUseScope() + if (useScope is GlobalSearchScope) { + val searchCostResult = psiSearchHelper.isCheapEnoughToSearch(declaration.getName(), useScope, null, null) + + 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 + } + } + val searchHelper: UsagesSearchHelper = when (declaration) { is JetClass -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true) is JetNamedFunction -> FunctionUsagesSearchHelper(skipImports = true) else -> return false } - val useScope = declaration.getUseScope() val request = searchHelper.newRequest(UsagesSearchTarget(declaration, useScope)) val query = UsagesSearch.search(request) diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.a.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.a.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.a.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.b.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.b.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.b.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.c.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.c.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.c.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.d.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.d.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.d.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.e.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.e.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.e.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.f.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.f.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.f.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.g.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.g.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.g.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.h.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.h.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.h.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.i.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.i.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.i.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.j.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.j.kt new file mode 100644 index 00000000000..b73e142d60b --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.j.kt @@ -0,0 +1 @@ +package unusedButHasTooPopularName.f diff --git a/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.kt b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.kt new file mode 100644 index 00000000000..bc0e5037230 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/function/unusedButHasTooPopularName.kt @@ -0,0 +1,4 @@ +package unusedButHasTooPopularName + +fun f() { // unused, but won't marked so: too expensive to check +}