Unused symbol: secondary constructors are also under analysis #KT-10812 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-05-11 14:46:28 +03:00
parent b7581b35c9
commit 8920e67c5a
7 changed files with 42 additions and 4 deletions
@@ -52,6 +52,7 @@ unused.object=Object ''{0}'' is never used
unused.function=Function ''{0}'' is never used unused.function=Function ''{0}'' is never used
unused.property=Property ''{0}'' is never used unused.property=Property ''{0}'' is never used
unused.type.parameter=Type parameter ''{0}'' is never used unused.type.parameter=Type parameter ''{0}'' is never used
unused.constructor=Constructor is never used
unused.receiver.parameter=Receiver parameter is never used unused.receiver.parameter=Receiver parameter is never used
unused.receiver.parameter.remove=Remove redundant receiver parameter unused.receiver.parameter.remove=Remove redundant receiver parameter
@@ -129,11 +129,13 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : KtVisitorVoid() { return object : KtVisitorVoid() {
override fun visitNamedDeclaration(declaration: KtNamedDeclaration) { override fun visitDeclaration(declaration: KtDeclaration) {
if (declaration !is KtNamedDeclaration) return
val messageKey = when (declaration) { val messageKey = when (declaration) {
is KtClass -> "unused.class" is KtClass -> "unused.class"
is KtObjectDeclaration -> "unused.object" is KtObjectDeclaration -> "unused.object"
is KtNamedFunction -> "unused.function" is KtNamedFunction -> "unused.function"
is KtSecondaryConstructor -> "unused.constructor"
is KtProperty, is KtParameter -> "unused.property" is KtProperty, is KtParameter -> "unused.property"
is KtTypeParameter -> "unused.type.parameter" is KtTypeParameter -> "unused.type.parameter"
else -> return else -> return
@@ -143,7 +145,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
// Simple PSI-based checks // Simple PSI-based checks
if (declaration is KtObjectDeclaration && declaration.isCompanion()) return // never mark companion object as unused (there are too many reasons it can be needed for) if (declaration is KtObjectDeclaration && declaration.isCompanion()) return // never mark companion object as unused (there are too many reasons it can be needed for)
if (declaration.name == null) return declaration.name ?: return
if (declaration is KtEnumEntry) return if (declaration is KtEnumEntry) return
if (declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return if (declaration.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return
if (declaration is KtProperty && declaration.isLocal) return if (declaration is KtProperty && declaration.isLocal) return
@@ -162,8 +164,9 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
if (hasNonTrivialUsages(declaration)) return if (hasNonTrivialUsages(declaration)) return
if (declaration is KtClassOrObject && classOrObjectHasTextUsages(declaration)) return if (declaration is KtClassOrObject && classOrObjectHasTextUsages(declaration)) return
val psiElement = declaration.nameIdentifier ?: (declaration as? KtConstructor<*>)?.getConstructorKeyword() ?: return
val problemDescriptor = holder.manager.createProblemDescriptor( val problemDescriptor = holder.manager.createProblemDescriptor(
declaration.nameIdentifier!!, psiElement,
null, null,
KotlinBundle.message(messageKey, declaration.name), KotlinBundle.message(messageKey, declaration.name),
ProblemHighlightType.LIKE_UNUSED_SYMBOL, ProblemHighlightType.LIKE_UNUSED_SYMBOL,
@@ -301,7 +304,9 @@ class UnusedSymbolInspection : AbstractKotlinInspection() {
} }
class SafeDeleteFix(val declaration: KtDeclaration) : LocalQuickFix { class SafeDeleteFix(val declaration: KtDeclaration) : LocalQuickFix {
override fun getName() = QuickFixBundle.message("safe.delete.text", declaration.name) override fun getName() =
if (declaration is KtConstructor<*>) "Safe delete constructor"
else QuickFixBundle.message("safe.delete.text", declaration.name)
override fun getFamilyName() = "Safe delete" override fun getFamilyName() = "Safe delete"
@@ -69,4 +69,13 @@
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class> <problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Class 'WithNativeAnnotation' is never used</description> <description>Class 'WithNativeAnnotation' is never used</description>
</problem> </problem>
<problem>
<file>secondaryConstructor.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/secondaryConstructor.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused symbol</problem_class>
<description>Constructor is never used</description>
</problem>
</problems> </problems>
@@ -0,0 +1,10 @@
class Owner(val name: String, val code: Int) {
constructor(name: String): this(name, 0)
constructor(code: Int): this("", code)
}
@test.anno.EntryPoint
fun use(): Int {
val owner = Owner("xyz")
return if (owner.name != "") owner.code else -1
}
@@ -0,0 +1,4 @@
// "Safe delete constructor" "true"
class Owner(val x: Int) {
<caret>constructor(): this(42)
}
@@ -0,0 +1,3 @@
// "Safe delete constructor" "true"
class Owner(val x: Int) {
}
@@ -6442,6 +6442,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("unusedConstructor.kt")
public void testUnusedConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/unusedConstructor.kt");
doTest(fileName);
}
@TestMetadata("unusedFunction.kt") @TestMetadata("unusedFunction.kt")
public void testUnusedFunction() throws Exception { public void testUnusedFunction() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/unusedFunction.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeUnused/unusedFunction.kt");