Fix UnusedSymbolInspection for parameterless and suspend main functions

#KT-26574 Fixed
This commit is contained in:
Denis Zharkov
2018-09-07 12:11:00 +03:00
parent 1cc0c12f87
commit aa74511378
8 changed files with 40 additions and 1 deletions
@@ -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)
@@ -0,0 +1,11 @@
<problems>
<problem>
<file>topLevelBothMains.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/topLevelBothMains.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused symbol</problem_class>
<description>Function &quot;main&quot; is never used</description>
</problem>
</problems>
@@ -0,0 +1,2 @@
// LANGUAGE_VERSION: 1.3
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection
@@ -0,0 +1,4 @@
package a
fun main() {}
fun main(args: Array<String>) {}
@@ -0,0 +1,3 @@
fun main() {
}
@@ -0,0 +1,3 @@
suspend fun main(args: Array<String>) {
}
@@ -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!!
@@ -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");