Do not mark kotlin.test.Test annotated declarations as unused

So #KT-20523 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-06-25 15:31:16 +03:00
parent 7e0e7dc983
commit 30327aa9cc
12 changed files with 92 additions and 0 deletions
@@ -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<String>
): Boolean {
if (declaration.annotationEntries.isEmpty()) return false
val context = declaration.analyze()
val annotationsPresent = declaration.annotationEntries.mapNotNull {
context[BindingContext.ANNOTATION, it]?.fqName?.asString()
@@ -0,0 +1,6 @@
package kotlin.test
annotation class Test
fun assertEquals(expected: Any, actual: Any) {}
@@ -0,0 +1,10 @@
import kotlin.test.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun testSomething() {
assertEquals(4, 2 + 2)
}
}
@@ -0,0 +1,6 @@
package kotlin.test
annotation class Test
fun assertEquals(expected: Any, actual: Any) {}
@@ -0,0 +1,10 @@
import kotlin.test.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun <caret>testSomething() {
assertEquals(4, 2 + 2)
}
}
@@ -0,0 +1,5 @@
{
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection",
"problem": "none",
"mainFile": "test.kt"
}
@@ -0,0 +1,6 @@
package kotlin.test
annotation class Test
fun assertEquals(expected: Any, actual: Any) {}
@@ -0,0 +1,10 @@
import kotlin.test.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun testSomething() {
assertEquals(4, 2 + 2)
}
}
@@ -0,0 +1,6 @@
package kotlin.test
annotation class Test
fun assertEquals(expected: Any, actual: Any) {}
@@ -0,0 +1,10 @@
import kotlin.test.Test
import kotlin.test.assertEquals
class <caret>SomeTest {
@Test
fun testSomething() {
assertEquals(4, 2 + 2)
}
}
@@ -0,0 +1,5 @@
{
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection",
"problem": "none",
"mainFile": "test.kt"
}
@@ -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");
}
}