Restrict search scope in some inspections to avoid testData search

Affected inspections: can be private, can be parameter, unused symbol.
Related to KT-21756. May fix EA-113712.
This commit is contained in:
Mikhail Glukhikh
2017-12-12 14:34:10 +03:00
parent 2b5bbf0fdd
commit ea5a52f918
3 changed files with 17 additions and 10 deletions
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.idea.quickfix.RemoveValVarFromParameterFix
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.idea.search.isCheapEnoughToSearchConsideringOperators
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
@@ -90,7 +91,7 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
if (klass.isData()) return
val useScope = parameter.useScope
if (useScope is GlobalSearchScope) {
val restrictedScope = if (useScope is GlobalSearchScope) {
val psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(parameter.project)
for (accessorName in parameter.getAccessorNames()) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(accessorName, useScope, null, null)) {
@@ -102,9 +103,11 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
// TOO_MANY_OCCURRENCES: too expensive
// ZERO_OCCURRENCES: unused at all, reported elsewhere
if (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null) != FEW_OCCURRENCES) return
KotlinSourceFilterScope.projectSources(useScope, parameter.project)
}
else useScope
// Find all references and check them
val references = ReferencesSearch.search(parameter, useScope)
val references = ReferencesSearch.search(parameter, restrictedScope)
if (references.none()) return
if (references.any { it.usedAsPropertyIn(klass) }) return
holder.registerProblem(
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.idea.core.toDescriptor
import org.jetbrains.kotlin.idea.quickfix.AddModifierFix
import org.jetbrains.kotlin.idea.refactoring.isConstructorDeclaredProperty
import org.jetbrains.kotlin.idea.search.isCheapEnoughToSearchConsideringOperators
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
@@ -97,18 +98,18 @@ class MemberVisibilityCanBePrivateInspection : AbstractKotlinInspection() {
val psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(declaration.project)
val useScope = declaration.useScope
val name = declaration.name ?: return false
if (useScope is GlobalSearchScope) {
val restrictedScope = if (useScope is GlobalSearchScope) {
when (psiSearchHelper.isCheapEnoughToSearchConsideringOperators(name, useScope, null, null)) {
PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES -> return false
PsiSearchHelper.SearchCostResult.ZERO_OCCURRENCES -> return false
PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES -> {
}
PsiSearchHelper.SearchCostResult.FEW_OCCURRENCES -> KotlinSourceFilterScope.projectSources(useScope, declaration.project)
}
}
else useScope
var otherUsageFound = false
var inClassUsageFound = false
ReferencesSearch.search(declaration, useScope).forEach(Processor<PsiReference> {
ReferencesSearch.search(declaration, restrictedScope).forEach(Processor<PsiReference> {
val usage = it.element
if (classOrObject != usage.getParentOfType<KtClassOrObject>(false)) {
otherUsageFound = true
@@ -67,6 +67,7 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.dataClassComponentFunction
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
import org.jetbrains.kotlin.idea.search.usagesSearch.getClassNameForCompanionObject
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
@@ -206,7 +207,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
val psiSearchHelper = PsiSearchHelper.SERVICE.getInstance(declaration.project)
val useScope = declaration.useScope
if (useScope is GlobalSearchScope) {
val restrictedScope = if (useScope is GlobalSearchScope) {
var zeroOccurrences = true
for (name in listOf(declaration.name) + declaration.getAccessorNames() + listOfNotNull(declaration.getClassNameForCompanionObject())) {
@@ -226,13 +227,15 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
return false
}
}
KotlinSourceFilterScope.projectSources(useScope, declaration.project)
}
else useScope
return (declaration is KtObjectDeclaration && declaration.isCompanion() &&
declaration.getBody()?.declarations?.isNotEmpty() == true) ||
hasReferences(declaration, descriptor, useScope) ||
hasOverrides(declaration, useScope) ||
hasFakeOverrides(declaration, useScope) ||
hasReferences(declaration, descriptor, restrictedScope) ||
hasOverrides(declaration, restrictedScope) ||
hasFakeOverrides(declaration, restrictedScope) ||
isPlatformImplementation(declaration) ||
hasPlatformImplementations(declaration, descriptor)
}