Finding unused properties.

This commit is contained in:
Evgeny Gerashchenko
2015-01-16 21:48:19 +03:00
parent 460fae275b
commit 8fd5e72632
15 changed files with 117 additions and 1 deletions
@@ -119,6 +119,7 @@ goto.super.class.chooser.title=Choose super class or interface
unused.class=Class ''{0}'' is never used
unused.function=Function ''{0}'' is never used
unused.property=Property ''{0}'' is never used
livetemplate.description.main=main() function
livetemplate.description.soutp=Prints function parameter names and values to System.out
@@ -44,6 +44,8 @@ import com.intellij.psi.search.PsiSearchHelper.SearchCostResult.*
import org.jetbrains.kotlin.idea.search.usagesSearch.getOperationSymbolsToSearch
import org.jetbrains.kotlin.idea.search.usagesSearch.INVOKE_OPERATION_NAME
import org.jetbrains.kotlin.psi.JetEnumEntry
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.idea.search.usagesSearch.PropertyUsagesSearchHelper
public class UnusedSymbolInspection : AbstractKotlinInspection() {
private val javaInspection = UnusedDeclarationInspection()
@@ -82,6 +84,21 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
ProblemHighlightType.LIKE_UNUSED_SYMBOL
) // TODO add quick fix to delete it
}
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
) // TODO add quick fix to delete it
}
}
}
@@ -132,6 +149,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
val searchHelper: UsagesSearchHelper<out JetNamedDeclaration> = when (declaration) {
is JetClass -> ClassUsagesSearchHelper(constructorUsages = true, nonConstructorUsages = true, skipImports = true)
is JetNamedFunction -> FunctionUsagesSearchHelper(skipImports = true)
is JetProperty -> PropertyUsagesSearchHelper(skipImports = true)
else -> return false
}
@@ -1,3 +1,5 @@
class A {
val x: A? = null
{
val x: A? = null
}
}
@@ -0,0 +1,9 @@
open class Klass {
val used = ":)"
}
class Subklass: Klass()
fun main(args: Array<String>) {
Subklass().used
}
@@ -0,0 +1,11 @@
open class Klass {
open val used = ":)"
}
class Subklass: Klass() {
override fun used = ":|"
}
fun main(args: Array<String>) {
Subklass().used
}
@@ -0,0 +1,12 @@
trait Trait {
val member: String
}
class Klass: Trait {
override val member: String = ":)"
}
fun main(args: Array<String>) {
val t: Trait = Klass()
println(t.member)
}
@@ -0,0 +1,7 @@
class Klass {
val unused = ":("
}
fun main(args: Array<String>) {
Klass()
}
@@ -0,0 +1,7 @@
class Klass {
fun used = ":)"
}
fun main(args: Array<String>) {
Klass().used
}
@@ -0,0 +1,28 @@
<problems>
<problem>
<file>topLevelUnused.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/topLevelUnused.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'unused' is never used</description>
</problem>
<problem>
<file>topLevelOnlyInImport.kt</file>
<line>5</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/topLevelOnlyInImport.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'onlyInImport' is never used</description>
</problem>
<problem>
<file>classMemberUnused.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/classMemberUnused.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'unused' is never used</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection
@@ -0,0 +1,3 @@
fun main(args: Array<String>) {
val local = ":(" // it is unused, but marked by front-end instead of inspection
}
@@ -0,0 +1,5 @@
package foo
import foo.onlyInImport
val onlyInImport = ":("
@@ -0,0 +1 @@
val unused = ":("
@@ -0,0 +1,5 @@
fun used = ":)"
fun main(args: Array<String>) {
println(used)
}
@@ -89,5 +89,11 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/function/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("unusedSymbol/property/inspectionData/inspections.test")
public void testUnusedSymbol_property_inspectionData_Inspections_test() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/property/inspectionData/inspections.test");
doTest(fileName);
}
}
}