Implemented simplest version of unused symbol inspection.
This commit is contained in:
@@ -451,6 +451,7 @@ fun main(args: Array<String>) {
|
||||
|
||||
testClass(javaClass<AbstractJetInspectionTest>()) {
|
||||
model("intentions", pattern = "^(inspections\\.test)$", singleClass = true)
|
||||
model("inspections", pattern = "^(inspections\\.test)$", singleClass = true)
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractHierarchyTest>()) {
|
||||
|
||||
@@ -117,6 +117,8 @@ goto.super.function.chooser.title=Choose super function
|
||||
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
|
||||
|
||||
livetemplate.description.main=main() function
|
||||
livetemplate.description.soutp=Prints function parameter names and values to System.out
|
||||
livetemplate.description.iter=Iterate over elements of iterable
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports classes, functions or properties in the specified inspection scope that are not used or not reachable from entry points.
|
||||
</body>
|
||||
</html>
|
||||
@@ -826,6 +826,13 @@
|
||||
level="WARNING"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection"
|
||||
displayName="Unused Symbol"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
/>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.kotlin.idea.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import org.jetbrains.kotlin.idea.findUsages.toClassHelper
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.codeInspection.LocalInspectionToolSession
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.psi.JetVisitorVoid
|
||||
import org.jetbrains.kotlin.psi.JetClass
|
||||
import org.jetbrains.kotlin.idea.findUsages.KotlinClassFindUsagesOptions
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchTarget
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearch
|
||||
import com.intellij.util.Processor
|
||||
import org.jetbrains.kotlin.idea.JetBundle
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
|
||||
public class UnusedSymbolInspection : AbstractKotlinInspection() {
|
||||
override fun runForWholeFile() = true
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
return object : JetVisitorVoid() {
|
||||
override fun visitClass(klass: JetClass) {
|
||||
if (klass.getName() == null) return
|
||||
|
||||
val usagesSearchHelper = KotlinClassFindUsagesOptions(holder.getProject()).toClassHelper()
|
||||
val request = usagesSearchHelper.newRequest(UsagesSearchTarget(klass, klass.getUseScope()))
|
||||
val query = UsagesSearch.search(request)
|
||||
|
||||
var foundNonTrivialUsage = false
|
||||
query.forEach(Processor {
|
||||
usage ->
|
||||
if (klass.isAncestor(usage.getElement())) {
|
||||
true
|
||||
}
|
||||
else {
|
||||
foundNonTrivialUsage = true
|
||||
false
|
||||
}
|
||||
})
|
||||
|
||||
if (!foundNonTrivialUsage) {
|
||||
holder.registerProblem(
|
||||
klass.getNameIdentifier(),
|
||||
JetBundle.message("unused.class", klass.getName()),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||
) // TODO add quick fix to delete it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<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>Class '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>Class 'A' is never used</description>
|
||||
</problem>
|
||||
<problem>
|
||||
<file>localUnused.kt</file>
|
||||
<line>2</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/localUnused.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
|
||||
<description>Class 'Local' is never used</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection
|
||||
@@ -0,0 +1,4 @@
|
||||
fun f() {
|
||||
class Local {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun f() {
|
||||
class Local {
|
||||
|
||||
}
|
||||
|
||||
Local()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
class A
|
||||
@@ -0,0 +1,4 @@
|
||||
class A
|
||||
|
||||
fun f(): A {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class A {
|
||||
fun f(other: A) {
|
||||
}
|
||||
}
|
||||
@@ -28,41 +28,60 @@ import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/intentions")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({JetInspectionTestGenerated.Intentions.class, JetInspectionTestGenerated.Inspections.class})
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class JetInspectionTestGenerated extends AbstractJetInspectionTest {
|
||||
public void testAllFilesPresentInIntentions() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/intentions"), Pattern.compile("^(inspections\\.test)$"));
|
||||
@TestMetadata("idea/testData/intentions")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Intentions extends AbstractJetInspectionTest {
|
||||
public void testAllFilesPresentInIntentions() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/intentions"), Pattern.compile("^(inspections\\.test)$"));
|
||||
}
|
||||
|
||||
@TestMetadata("attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test")
|
||||
public void testAttributeCallReplacements_replaceGetIntention_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("branched/ifThenToElvis/inspectionData/inspections.test")
|
||||
public void testBranched_ifThenToElvis_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("branched/ifThenToSafeAccess/inspectionData/inspections.test")
|
||||
public void testBranched_ifThenToSafeAccess_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeExplicitTypeArguments/inspectionData/inspections.test")
|
||||
public void testRemoveExplicitTypeArguments_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simplifyNegatedBinaryExpressionIntention/inspectionData/inspections.test")
|
||||
public void testSimplifyNegatedBinaryExpressionIntention_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/simplifyNegatedBinaryExpressionIntention/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test")
|
||||
public void testAttributeCallReplacements_replaceGetIntention_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
@TestMetadata("idea/testData/inspections")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Inspections extends AbstractJetInspectionTest {
|
||||
public void testAllFilesPresentInInspections() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File("idea/testData/inspections"), Pattern.compile("^(inspections\\.test)$"));
|
||||
}
|
||||
|
||||
@TestMetadata("branched/ifThenToElvis/inspectionData/inspections.test")
|
||||
public void testBranched_ifThenToElvis_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("branched/ifThenToSafeAccess/inspectionData/inspections.test")
|
||||
public void testBranched_ifThenToSafeAccess_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeExplicitTypeArguments/inspectionData/inspections.test")
|
||||
public void testRemoveExplicitTypeArguments_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simplifyNegatedBinaryExpressionIntention/inspectionData/inspections.test")
|
||||
public void testSimplifyNegatedBinaryExpressionIntention_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/simplifyNegatedBinaryExpressionIntention/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
@TestMetadata("unusedSymbol/class/inspectionData/inspections.test")
|
||||
public void testUnusedSymbol_class_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/class/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user