From 339868309334d0356fcc4928816806e87968d5f0 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 13 Feb 2020 19:27:27 +0900 Subject: [PATCH] Unused symbol: do not report for secondary constructor when class is used as typealias #KT-20907 Fixed --- .../idea/inspections/UnusedSymbolInspection.kt | 16 +++++++++------- .../secondaryConstructorCalledByTypeAlias.kt | 10 ++++++++++ .../LocalInspectionTestGenerated.java | 5 +++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 idea/testData/inspectionsLocal/unusedSymbol/secondaryConstructorCalledByTypeAlias.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 4860767d317..80d90246673 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -383,6 +383,13 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (referenceUsed) return true } + if (declaration is KtSecondaryConstructor) { + val containingClass = declaration.containingClass() + if (containingClass != null && ReferencesSearch.search(KotlinReferencesSearchParameters(containingClass, useScope)).any { + it.element.getStrictParentOfType() != null + }) return true + } + if (declaration is KtCallableDeclaration && declaration.canBeHandledByLightMethods(descriptor)) { val lightMethods = declaration.toLightMethods() if (lightMethods.isNotEmpty()) { @@ -396,13 +403,8 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { if (declaration is KtEnumEntry) { val enumClass = declaration.containingClass()?.takeIf { it.isEnum() } - if (enumClass != null && ReferencesSearch.search( - KotlinReferencesSearchParameters( - enumClass, - useScope, - kotlinOptions = searchOptions - ) - ).any(::hasBuiltInEnumFunctionReference) + if (enumClass != null + && ReferencesSearch.search(KotlinReferencesSearchParameters(enumClass, useScope)).any(::hasBuiltInEnumFunctionReference) ) return true } diff --git a/idea/testData/inspectionsLocal/unusedSymbol/secondaryConstructorCalledByTypeAlias.kt b/idea/testData/inspectionsLocal/unusedSymbol/secondaryConstructorCalledByTypeAlias.kt new file mode 100644 index 00000000000..15b4c062159 --- /dev/null +++ b/idea/testData/inspectionsLocal/unusedSymbol/secondaryConstructorCalledByTypeAlias.kt @@ -0,0 +1,10 @@ +// PROBLEM: none +class A(x: Double) { + constructor(i: Int) : this(i.toDouble()) +} + +class C { + val a = B(1) +} + +typealias B = A \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 7b6666f06c3..6a4f2ece4ee 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -13064,6 +13064,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/unusedSymbol/propertyOfInlineClassType.kt"); } + @TestMetadata("secondaryConstructorCalledByTypeAlias.kt") + public void testSecondaryConstructorCalledByTypeAlias() throws Exception { + runTest("idea/testData/inspectionsLocal/unusedSymbol/secondaryConstructorCalledByTypeAlias.kt"); + } + @TestMetadata("secondaryLocalClassConstructor.kt") public void testSecondaryLocalClassConstructor() throws Exception { runTest("idea/testData/inspectionsLocal/unusedSymbol/secondaryLocalClassConstructor.kt");