From aa74511378075f64158309bc96941569d38c3daf Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 7 Sep 2018 12:11:00 +0300 Subject: [PATCH] Fix UnusedSymbolInspection for parameterless and suspend main functions #KT-26574 Fixed --- .../kotlin/idea/inspections/UnusedSymbolInspection.kt | 7 +++++++ .../functionMain/inspectionData/expected.xml | 11 +++++++++++ .../functionMain/inspectionData/inspections.test | 2 ++ .../unusedSymbol/functionMain/topLevelBothMains.kt | 4 ++++ .../functionMain/topLevelParameterlessMain.kt | 3 +++ .../unusedSymbol/functionMain/topLevelSuspendMain.kt | 3 +++ .../kotlin/idea/codeInsight/AbstractInspectionTest.kt | 6 +++++- .../idea/codeInsight/InspectionTestGenerated.java | 5 +++++ 8 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspections/unusedSymbol/functionMain/inspectionData/expected.xml create mode 100644 idea/testData/inspections/unusedSymbol/functionMain/inspectionData/inspections.test create mode 100644 idea/testData/inspections/unusedSymbol/functionMain/topLevelBothMains.kt create mode 100644 idea/testData/inspections/unusedSymbol/functionMain/topLevelParameterlessMain.kt create mode 100644 idea/testData/inspections/unusedSymbol/functionMain/topLevelSuspendMain.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt index 3fdf576135d..2eb6dec197f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnusedSymbolInspection.kt @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.idea.core.toDescriptor import org.jetbrains.kotlin.idea.findUsages.KotlinFindUsagesHandlerFactory import org.jetbrains.kotlin.idea.findUsages.handlers.KotlinFindClassUsagesHandler import org.jetbrains.kotlin.idea.imports.importableFqName +import org.jetbrains.kotlin.idea.isMainFunction import org.jetbrains.kotlin.idea.project.languageVersionSettings import org.jetbrains.kotlin.idea.quickfix.RemoveUnusedFunctionParameterFix import org.jetbrains.kotlin.idea.references.mainReference @@ -86,6 +87,12 @@ class UnusedSymbolInspection : AbstractKotlinInspection() { fun isEntryPoint(declaration: KtNamedDeclaration): Boolean { if (declaration.hasKotlinAdditionalAnnotation()) return true if (declaration is KtClass && declaration.declarations.any { it.hasKotlinAdditionalAnnotation() }) return true + + // Some of the main-function-cases are covered by 'javaInspection.isEntryPoint(lightElement)' call + // but not all of them: light method for parameterless main still points to parameterless name + // that is not an actual entry point from Java language point of view + if (declaration.isMainFunction()) return true + val lightElement: PsiElement? = when (declaration) { is KtClassOrObject -> declaration.toLightClass() is KtNamedFunction, is KtSecondaryConstructor -> LightClassUtil.getLightClassMethod(declaration as KtFunction) diff --git a/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/expected.xml b/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/expected.xml new file mode 100644 index 00000000000..679bea80058 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/expected.xml @@ -0,0 +1,11 @@ + + + topLevelBothMains.kt + 3 + light_idea_test_case + <default> + + Unused symbol + Function "main" is never used + + diff --git a/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/inspections.test b/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/inspections.test new file mode 100644 index 00000000000..b137ab1cb93 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/functionMain/inspectionData/inspections.test @@ -0,0 +1,2 @@ +// LANGUAGE_VERSION: 1.3 +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection diff --git a/idea/testData/inspections/unusedSymbol/functionMain/topLevelBothMains.kt b/idea/testData/inspections/unusedSymbol/functionMain/topLevelBothMains.kt new file mode 100644 index 00000000000..2e7c275e1f8 --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/functionMain/topLevelBothMains.kt @@ -0,0 +1,4 @@ +package a + +fun main() {} +fun main(args: Array) {} diff --git a/idea/testData/inspections/unusedSymbol/functionMain/topLevelParameterlessMain.kt b/idea/testData/inspections/unusedSymbol/functionMain/topLevelParameterlessMain.kt new file mode 100644 index 00000000000..32f47fee75a --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/functionMain/topLevelParameterlessMain.kt @@ -0,0 +1,3 @@ +fun main() { + +} diff --git a/idea/testData/inspections/unusedSymbol/functionMain/topLevelSuspendMain.kt b/idea/testData/inspections/unusedSymbol/functionMain/topLevelSuspendMain.kt new file mode 100644 index 00000000000..ceee0b6cdfa --- /dev/null +++ b/idea/testData/inspections/unusedSymbol/functionMain/topLevelSuspendMain.kt @@ -0,0 +1,3 @@ +suspend fun main(args: Array) { + +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractInspectionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractInspectionTest.kt index 8b2da9f6c79..a0ee6b50546 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractInspectionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractInspectionTest.kt @@ -25,7 +25,9 @@ import com.intellij.testFramework.TestLoggerFactory import org.jdom.Document import org.jdom.input.SAXBuilder import org.jetbrains.kotlin.idea.inspections.runInspection -import org.jetbrains.kotlin.idea.test.* +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.TestFixtureExtension +import org.jetbrains.kotlin.idea.test.configureCompilerOptions import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion import org.jetbrains.kotlin.test.InTextDirectivesUtils @@ -68,6 +70,8 @@ abstract class AbstractInspectionTest : KotlinLightCodeInsightFixtureTestCase() val fixtureClasses = InTextDirectivesUtils.findListWithPrefixes(options, "// FIXTURE_CLASS: ") + configureCompilerOptions(options, project, module) + val inspectionsTestDir = optionsFile.parentFile!! val srcDir = inspectionsTestDir.parentFile!! diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java index bf1c3be5f7b..5b4a7fd67a1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/InspectionTestGenerated.java @@ -409,6 +409,11 @@ public class InspectionTestGenerated extends AbstractInspectionTest { runTest("idea/testData/inspections/unusedSymbol/function/inspectionData/inspections.test"); } + @TestMetadata("unusedSymbol/functionMain/inspectionData/inspections.test") + public void testUnusedSymbol_functionMain_inspectionData_Inspections_test() throws Exception { + runTest("idea/testData/inspections/unusedSymbol/functionMain/inspectionData/inspections.test"); + } + @TestMetadata("unusedSymbol/js/inspectionData/inspections.test") public void testUnusedSymbol_js_inspectionData_Inspections_test() throws Exception { runTest("idea/testData/inspections/unusedSymbol/js/inspectionData/inspections.test");