Searching for properties declared in class constructor signature.
This commit is contained in:
@@ -47,6 +47,8 @@ import org.jetbrains.kotlin.psi.JetEnumEntry
|
||||
import org.jetbrains.kotlin.psi.JetProperty
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.PropertyUsagesSearchHelper
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
|
||||
import org.jetbrains.kotlin.psi.JetParameter
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.dataClassComponentFunctionName
|
||||
|
||||
public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
private val javaInspection = UnusedDeclarationInspection()
|
||||
@@ -100,6 +102,24 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||
) // TODO add quick fix to delete it
|
||||
}
|
||||
|
||||
override fun visitParameter(parameter: JetParameter) {
|
||||
if (parameter.getName() == null) return
|
||||
|
||||
if (parameter.getParent()?.getParent() !is JetClass || !parameter.hasValOrVarNode()) return
|
||||
|
||||
// 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 (parameter.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
|
||||
if (hasNonTrivialUsages(parameter)) return
|
||||
|
||||
holder.registerProblem(
|
||||
parameter.getNameIdentifier(),
|
||||
JetBundle.message("unused.property", parameter.getName()),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||
) // TODO add quick fix to delete it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +176,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)
|
||||
is JetProperty, is JetParameter -> PropertyUsagesSearchHelper(skipImports = true)
|
||||
else -> return false
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>paramPropertyUnused.kt</file>
|
||||
<line>1</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/paramPropertyUnused.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
|
||||
<description>Property 'param' is never used</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection
|
||||
@@ -0,0 +1,6 @@
|
||||
// properties can be referred by component1/component2, which is too expensive to search, don't mark them as unused
|
||||
data class MyClass(val param1: String, var param2: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
MyClass()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
open class Klass {
|
||||
open val used = ":)"
|
||||
}
|
||||
|
||||
class Subklass: Klass(override val used: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Subklass().used
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
trait Trait {
|
||||
val member: String
|
||||
}
|
||||
|
||||
class Klass(override val member: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val t: Trait = Klass()
|
||||
println(t.member)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class MyClass(val param: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
MyClass()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class MyClass(val param: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
MyClass().param
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package test;
|
||||
|
||||
import foo.UsedInJava;
|
||||
|
||||
class usedInJava {
|
||||
public static void main(String[] args) {
|
||||
new UsedInJava("a", "b").getUsedByGetter();
|
||||
new UsedInJava("a", "b").setUsedBySetter(":|");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package foo
|
||||
|
||||
class UsedInJava(val usedByGetter: String, var usedBySetter: String)
|
||||
@@ -90,6 +90,12 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest {
|
||||
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");
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user