Unused object.

unusedButEntryPointMain for class was actually a wrong test: it didn't check anything, because objects are were not checked for unusedness before.
This commit is contained in:
Evgeny Gerashchenko
2015-01-21 21:36:57 +03:00
parent a797f4a328
commit 386854d73f
19 changed files with 105 additions and 12 deletions
@@ -118,6 +118,7 @@ goto.super.property.chooser.title=Choose super property
goto.super.class.chooser.title=Choose super class or interface
unused.class=Class ''{0}'' is never used
unused.object=Object ''{0}'' is never used
unused.function=Function ''{0}'' is never used
unused.property=Property ''{0}'' is never used
unused.type.parameter=Type parameter ''{0}'' is never used
@@ -59,6 +59,8 @@ import com.intellij.codeInsight.daemon.QuickFixBundle
import com.intellij.codeInsight.FileModificationService
import com.intellij.refactoring.safeDelete.SafeDeleteHandler
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetObjectDeclaration
import org.jetbrains.kotlin.psi.JetClassOrObject
public class UnusedSymbolInspection : AbstractKotlinInspection() {
private val javaInspection = UnusedDeclarationInspection()
@@ -83,6 +85,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
override fun visitNamedDeclaration(declaration: JetNamedDeclaration) {
val messageKey = when (declaration) {
is JetClass -> "unused.class"
is JetObjectDeclaration -> "unused.object"
is JetNamedFunction -> "unused.function"
is JetProperty, is JetParameter -> "unused.property"
is JetTypeParameter -> "unused.type.parameter"
@@ -104,7 +107,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
// Main checks: finding reference usages && text usages
if (hasNonTrivialUsages(declaration)) return
if (declaration is JetClass && classHasTextUsages(declaration)) return
if (declaration is JetClassOrObject && classOrObjectHasTextUsages(declaration)) return
holder.registerProblem(
declaration.getNameIdentifier(),
@@ -118,23 +121,23 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
private fun isEntryPoint(declaration: JetNamedDeclaration): Boolean {
val lightElement: PsiElement? = when (declaration) {
is JetClass -> declaration.toLightClass()
is JetClassOrObject -> declaration.toLightClass()
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
else -> return false
}
return lightElement != null && javaInspection.isEntryPoint(lightElement)
}
private fun classHasTextUsages(klass: JetClass): Boolean {
private fun classOrObjectHasTextUsages(classOrObject: JetClassOrObject): Boolean {
var hasTextUsages = false
// Finding text usages
if (klass.getUseScope() is GlobalSearchScope) {
val findClassUsagesHandler = KotlinFindClassUsagesHandler(klass, KotlinFindUsagesHandlerFactory(klass.getProject()))
if (classOrObject.getUseScope() is GlobalSearchScope) {
val findClassUsagesHandler = KotlinFindClassUsagesHandler(classOrObject, KotlinFindUsagesHandlerFactory(classOrObject.getProject()))
findClassUsagesHandler.processUsagesInText(
klass,
classOrObject,
{ hasTextUsages = true; false },
GlobalSearchScope.projectScope(klass.getProject())
GlobalSearchScope.projectScope(classOrObject.getProject())
)
}
@@ -1,5 +0,0 @@
object EntryPoint {
fun main(args: Array<String>) {
}
}
@@ -0,0 +1,34 @@
<problems>
<problem>
<file>unused.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/unused.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Object 'A' is never used</description>
</problem>
<problem>
<file>usedOnlyInside.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/usedOnlyInside.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Object 'A' is never used</description>
</problem>
<problem>
<file>usedInTextByShortName.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/usedInTextByShortName.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Object 'UsedInTextByShortName' is never used</description>
</problem>
<problem>
<file>usedOnlyInImport.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/usedOnlyInImport.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Object 'A' is never used</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection
@@ -0,0 +1 @@
object A
@@ -0,0 +1,11 @@
package entryPoint
// WITH_RUNTIME
import kotlin.platform.platformStatic
object EntryPoint {
platformStatic public fun main(args: Array<String>) {
}
}
@@ -0,0 +1,5 @@
object A
fun main(args: Array<String>) {
val x = A
}
@@ -0,0 +1,5 @@
class RandomJavaClass {
void f() {
foo.UsedInJava.INSTANCE$.bar();
}
}
@@ -0,0 +1,6 @@
package foo
object UsedInJava {
fun bar() {
}
}
@@ -0,0 +1,3 @@
package foo
object UsedInTextOnly
@@ -0,0 +1 @@
foo.UsedInTextOnly
@@ -0,0 +1,3 @@
package foo
object UsedInTextByShortName
@@ -0,0 +1 @@
UsedInTextByShortName
@@ -0,0 +1,3 @@
package foo
object UsedInXmlOnly
@@ -0,0 +1 @@
<a b="foo.UsedInXmlOnly"/>
@@ -0,0 +1,4 @@
import A
object A {
}
@@ -0,0 +1,9 @@
object A {
fun foo() {
A.bar()
}
fun bar() {
A.foo()
}
}
@@ -90,6 +90,12 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest {
doTest(fileName);
}
@TestMetadata("unusedSymbol/object/inspectionData/inspections.test")
public void testUnusedSymbol_object_inspectionData_Inspections_test() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/object/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("unusedSymbol/parameter/inspectionData/inspections.test")
public void testUnusedSymbol_parameter_inspectionData_Inspections_test() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/parameter/inspectionData/inspections.test");