From c49770d9a78879da31a5961aeb976d53f3cdde69 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 19 Nov 2018 16:39:39 +0900 Subject: [PATCH] Unused symbol: don't report for type parameter in open class #KT-23639 Fixed --- .../inspections/UnusedSymbolInspection.kt | 11 ++++++ .../typeParameter/inspectionData/expected.xml | 36 +++++++++++++++++++ .../typeParameter/openClassTypeParameter.kt | 33 +++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 idea/testData/inspections/unusedSymbol/typeParameter/openClassTypeParameter.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 7d6221fe385..3104e068a3a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -251,6 +251,17 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { } } else useScope + if (declaration is KtTypeParameter) { + val containingClass = declaration.containingClass() + if (containingClass != null) { + val isOpenClass = containingClass.isInterface() + || containingClass.hasModifier(KtTokens.ABSTRACT_KEYWORD) + || containingClass.hasModifier(KtTokens.SEALED_KEYWORD) + || containingClass.hasModifier(KtTokens.OPEN_KEYWORD) + if (isOpenClass && hasOverrides(containingClass, restrictedScope)) return true + } + } + return (declaration is KtObjectDeclaration && declaration.isCompanion() && declaration.getBody()?.declarations?.isNotEmpty() == true) || hasReferences(declaration, descriptor, restrictedScope) || diff --git a/idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/expected.xml b/idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/expected.xml index 42512da212b..85fc195c7b2 100644 --- a/idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/expected.xml +++ b/idea/testData/inspections/unusedSymbol/typeParameter/inspectionData/expected.xml @@ -16,4 +16,40 @@ Unused Symbol Type parameter 'T' is never used + + + openClassTypeParameter.kt + 1 + light_idea_test_case + + Unused Symbol + Type parameter 'T' is never used + + + + openClassTypeParameter.kt + 5 + light_idea_test_case + + Unused Symbol + Type parameter 'T' is never used + + + + openClassTypeParameter.kt + 9 + light_idea_test_case + + Unused Symbol + Type parameter 'T' is never used + + + + openClassTypeParameter.kt + 13 + light_idea_test_case + + Unused Symbol + Type parameter 'T' is never used + diff --git a/idea/testData/inspections/unusedSymbol/typeParameter/openClassTypeParameter.kt b/idea/testData/inspections/unusedSymbol/typeParameter/openClassTypeParameter.kt new file mode 100644 index 00000000000..7edcff0fdb2 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/typeParameter/openClassTypeParameter.kt @@ -0,0 +1,33 @@ +interface Interface +interface Interface2 +class A : Interface2 + +abstract class Abstract +abstract class Abstract2 +class B : Abstract2() + +sealed class Sealed +sealed class Sealed2 +class C : Sealed2() + +open class Open +open class Open2 +class D : Open2() + +fun main(args: Array) { + Interface::class + Interface2::class + A::class + + Abstract::class + Abstract2::class + B::class + + Sealed::class + Sealed2::class + C::class + + Open::class + Open2::class + D::class +} \ No newline at end of file