From 30327aa9ccf756e45f29ff2b98c4e6242b1a16e0 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 25 Jun 2018 15:31:16 +0300 Subject: [PATCH] Do not mark kotlin.test.Test annotated declarations as unused So #KT-20523 Fixed --- .../kotlin/idea/inspections/UnusedSymbolInspection.kt | 8 ++++++++ .../fromKotlinTest/after/kotlin/test/KotlinTest.kt | 6 ++++++ .../unusedSymbol/fromKotlinTest/after/test.kt | 10 ++++++++++ .../fromKotlinTest/before/kotlin/test/KotlinTest.kt | 6 ++++++ .../unusedSymbol/fromKotlinTest/before/test.kt | 10 ++++++++++ .../unusedSymbol/fromKotlinTest/fromKotlinTest.test | 5 +++++ .../after/kotlin/test/KotlinTest.kt | 6 ++++++ .../unusedSymbol/fromKotlinTestClass/after/test.kt | 10 ++++++++++ .../before/kotlin/test/KotlinTest.kt | 6 ++++++ .../unusedSymbol/fromKotlinTestClass/before/test.kt | 10 ++++++++++ .../fromKotlinTestClass/fromKotlinTestClass.test | 5 +++++ .../MultiFileLocalInspectionTestGenerated.java | 10 ++++++++++ 12 files changed, 92 insertions(+) create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/kotlin/test/KotlinTest.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/test.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/kotlin/test/KotlinTest.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/test.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/kotlin/test/KotlinTest.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/test.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/kotlin/test/KotlinTest.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/test.kt create mode 100644 idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 4df2237f3aa..130d0df2934 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -76,7 +76,14 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { companion object { private val javaInspection = UnusedDeclarationInspection() + private val KOTLIN_ADDITIONAL_ANNOTATIONS = listOf("kotlin.test.*") + + private fun KtDeclaration.hasKotlinAdditionalAnnotation() = + this is KtNamedDeclaration && checkAnnotatedUsingPatterns(this, KOTLIN_ADDITIONAL_ANNOTATIONS) + fun isEntryPoint(declaration: KtNamedDeclaration): Boolean { + if (declaration.hasKotlinAdditionalAnnotation()) return true + if (declaration is KtClass && declaration.declarations.any { it.hasKotlinAdditionalAnnotation() }) return true val lightElement: PsiElement? = when (declaration) { is KtClassOrObject -> declaration.toLightClass() is KtNamedFunction, is KtSecondaryConstructor -> LightClassUtil.getLightClassMethod(declaration as KtFunction) @@ -111,6 +118,7 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { declaration: KtNamedDeclaration, annotationPatterns: Collection ): Boolean { + if (declaration.annotationEntries.isEmpty()) return false val context = declaration.analyze() val annotationsPresent = declaration.annotationEntries.mapNotNull { context[BindingContext.ANNOTATION, it]?.fqName?.asString() diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/kotlin/test/KotlinTest.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/kotlin/test/KotlinTest.kt new file mode 100644 index 00000000000..51231dbaf0e --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/kotlin/test/KotlinTest.kt @@ -0,0 +1,6 @@ +package kotlin.test + +annotation class Test + +fun assertEquals(expected: Any, actual: Any) {} + diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/test.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/test.kt new file mode 100644 index 00000000000..a7ad9405a35 --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/after/test.kt @@ -0,0 +1,10 @@ +import kotlin.test.Test +import kotlin.test.assertEquals + +class SomeTest { + + @Test + fun testSomething() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/kotlin/test/KotlinTest.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/kotlin/test/KotlinTest.kt new file mode 100644 index 00000000000..51231dbaf0e --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/kotlin/test/KotlinTest.kt @@ -0,0 +1,6 @@ +package kotlin.test + +annotation class Test + +fun assertEquals(expected: Any, actual: Any) {} + diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/test.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/test.kt new file mode 100644 index 00000000000..bdeb1a25341 --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/before/test.kt @@ -0,0 +1,10 @@ +import kotlin.test.Test +import kotlin.test.assertEquals + +class SomeTest { + + @Test + fun testSomething() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test new file mode 100644 index 00000000000..c2bdad89914 --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test @@ -0,0 +1,5 @@ +{ + "inspectionClass": "org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection", + "problem": "none", + "mainFile": "test.kt" +} \ No newline at end of file diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/kotlin/test/KotlinTest.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/kotlin/test/KotlinTest.kt new file mode 100644 index 00000000000..51231dbaf0e --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/kotlin/test/KotlinTest.kt @@ -0,0 +1,6 @@ +package kotlin.test + +annotation class Test + +fun assertEquals(expected: Any, actual: Any) {} + diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/test.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/test.kt new file mode 100644 index 00000000000..a7ad9405a35 --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/after/test.kt @@ -0,0 +1,10 @@ +import kotlin.test.Test +import kotlin.test.assertEquals + +class SomeTest { + + @Test + fun testSomething() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/kotlin/test/KotlinTest.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/kotlin/test/KotlinTest.kt new file mode 100644 index 00000000000..51231dbaf0e --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/kotlin/test/KotlinTest.kt @@ -0,0 +1,6 @@ +package kotlin.test + +annotation class Test + +fun assertEquals(expected: Any, actual: Any) {} + diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/test.kt b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/test.kt new file mode 100644 index 00000000000..3208f4b8a8a --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/before/test.kt @@ -0,0 +1,10 @@ +import kotlin.test.Test +import kotlin.test.assertEquals + +class SomeTest { + + @Test + fun testSomething() { + assertEquals(4, 2 + 2) + } +} \ No newline at end of file diff --git a/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test new file mode 100644 index 00000000000..c2bdad89914 --- /dev/null +++ b/idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test @@ -0,0 +1,5 @@ +{ + "inspectionClass": "org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection", + "problem": "none", + "mainFile": "test.kt" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/MultiFileLocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/MultiFileLocalInspectionTestGenerated.java index 049f7f2f594..af6d9fbe6ce 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/MultiFileLocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/MultiFileLocalInspectionTestGenerated.java @@ -78,4 +78,14 @@ public class MultiFileLocalInspectionTestGenerated extends AbstractMultiFileLoca public void testReconcilePackageWithDirectory_packageMatchesDirectory_PackageMatchesDirectory() throws Exception { runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/packageMatchesDirectory/packageMatchesDirectory.test"); } + + @TestMetadata("unusedSymbol/fromKotlinTest/fromKotlinTest.test") + public void testUnusedSymbol_fromKotlinTest_FromKotlinTest() throws Exception { + runTest("idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test"); + } + + @TestMetadata("unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test") + public void testUnusedSymbol_fromKotlinTestClass_FromKotlinTestClass() throws Exception { + runTest("idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTestClass/fromKotlinTestClass.test"); + } }