Minor. Reduced code duplication.
This commit is contained in:
@@ -80,84 +80,37 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitClass(klass: JetClass) {
|
override fun visitNamedDeclaration(declaration: JetNamedDeclaration) {
|
||||||
if (klass.getName() == null) return
|
val messageKey = when (declaration) {
|
||||||
|
is JetClass -> "unused.class"
|
||||||
|
is JetNamedFunction -> "unused.function"
|
||||||
|
is JetProperty, is JetParameter -> "unused.property"
|
||||||
|
is JetTypeParameter -> "unused.type.parameter"
|
||||||
|
else -> return
|
||||||
|
}
|
||||||
|
|
||||||
if (klass is JetEnumEntry) return
|
// Simple PSI-based checks
|
||||||
|
if (declaration.getName() == null) return
|
||||||
if (isEntryPoint(klass)) return
|
if (declaration is JetEnumEntry) return
|
||||||
if (hasNonTrivialUsages(klass)) return
|
if (declaration.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
|
||||||
if (classHasTextUsages(klass)) return
|
if (declaration is JetProperty && declaration.isLocal()) return
|
||||||
|
if (declaration is JetParameter && (declaration.getParent()?.getParent() !is JetClass || !declaration.hasValOrVarNode())) return
|
||||||
holder.registerProblem(
|
if (declaration is JetNamedFunction && isConventionalName(declaration)) return
|
||||||
klass.getNameIdentifier(),
|
|
||||||
JetBundle.message("unused.class", klass.getName()),
|
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
|
||||||
createQuickFix(klass)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitNamedFunction(function: JetNamedFunction) {
|
|
||||||
if (function.getName() == null) return
|
|
||||||
|
|
||||||
if (function.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
|
|
||||||
if (isEntryPoint(function)) return
|
|
||||||
if (isConventionalName(function)) return
|
|
||||||
if (hasNonTrivialUsages(function)) return
|
|
||||||
|
|
||||||
holder.registerProblem(
|
|
||||||
function.getNameIdentifier(),
|
|
||||||
JetBundle.message("unused.function", function.getName()),
|
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
|
||||||
createQuickFix(function)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitProperty(property: JetProperty) {
|
|
||||||
if (property.getName() == null) return
|
|
||||||
|
|
||||||
if (property.isLocal()) return
|
|
||||||
|
|
||||||
if (property.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
|
|
||||||
if (hasNonTrivialUsages(property)) return
|
|
||||||
|
|
||||||
holder.registerProblem(
|
|
||||||
property.getNameIdentifier(),
|
|
||||||
JetBundle.message("unused.property", property.getName()),
|
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
|
||||||
createQuickFix(property)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitParameter(parameter: JetParameter) {
|
|
||||||
if (parameter.getName() == null) return
|
|
||||||
|
|
||||||
if (parameter.getParent()?.getParent() !is JetClass || !parameter.hasValOrVarNode()) return
|
|
||||||
|
|
||||||
|
// More expensive, resolve-based checks
|
||||||
|
if (isEntryPoint(declaration)) return
|
||||||
// properties can be referred by component1/component2, which is too expensive to search, don't mark them as unused
|
// properties can be referred by component1/component2, which is too expensive to search, don't mark them as unused
|
||||||
if (parameter.dataClassComponentFunctionName() != null) return
|
if (declaration is JetParameter && declaration.dataClassComponentFunctionName() != null) return
|
||||||
|
|
||||||
if (parameter.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
|
// Main checks: finding reference usages && text usages
|
||||||
if (hasNonTrivialUsages(parameter)) return
|
if (hasNonTrivialUsages(declaration)) return
|
||||||
|
if (declaration is JetClass && classHasTextUsages(declaration)) return
|
||||||
|
|
||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
parameter.getNameIdentifier(),
|
declaration.getNameIdentifier(),
|
||||||
JetBundle.message("unused.property", parameter.getName()),
|
JetBundle.message(messageKey, declaration.getName()),
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
||||||
createQuickFix(parameter)
|
createQuickFix(declaration)
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitTypeParameter(parameter: JetTypeParameter) {
|
|
||||||
if (parameter.getName() == null) return
|
|
||||||
|
|
||||||
if (hasNonTrivialUsages(parameter)) return
|
|
||||||
|
|
||||||
holder.registerProblem(
|
|
||||||
parameter.getNameIdentifier(),
|
|
||||||
JetBundle.message("unused.type.parameter", parameter.getName()),
|
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
|
||||||
createQuickFix(parameter)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,7 +120,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
|||||||
val lightElement: PsiElement? = when (declaration) {
|
val lightElement: PsiElement? = when (declaration) {
|
||||||
is JetClass -> declaration.toLightClass()
|
is JetClass -> declaration.toLightClass()
|
||||||
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
|
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
|
||||||
else -> null
|
else -> return false
|
||||||
}
|
}
|
||||||
return lightElement != null && javaInspection.isEntryPoint(lightElement)
|
return lightElement != null && javaInspection.isEntryPoint(lightElement)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user