Add inspection "main should return Unit" #KT-16085 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
8784d89f38
commit
1cd438c3f7
@@ -101,7 +101,11 @@ class MainFunctionDetector {
|
||||
|
||||
companion object {
|
||||
|
||||
fun isMain(descriptor: DeclarationDescriptor, checkJvmStaticAnnotation: Boolean = true): Boolean {
|
||||
fun isMain(
|
||||
descriptor: DeclarationDescriptor,
|
||||
checkJvmStaticAnnotation: Boolean = true,
|
||||
checkReturnType: Boolean = true
|
||||
): Boolean {
|
||||
if (descriptor !is FunctionDescriptor) return false
|
||||
|
||||
if (getJVMFunctionName(descriptor) != "main") {
|
||||
@@ -127,8 +131,7 @@ class MainFunctionDetector {
|
||||
return false
|
||||
}
|
||||
|
||||
val returnType = descriptor.returnType
|
||||
if (returnType == null || !KotlinBuiltIns.isUnit(returnType)) return false
|
||||
if (checkReturnType && !isMainReturnType(descriptor)) return false
|
||||
|
||||
if (DescriptorUtils.isTopLevelDeclaration(descriptor)) return true
|
||||
|
||||
@@ -138,6 +141,11 @@ class MainFunctionDetector {
|
||||
&& (descriptor.hasJvmStaticAnnotation() || !checkJvmStaticAnnotation)
|
||||
}
|
||||
|
||||
fun isMainReturnType(descriptor: FunctionDescriptor): Boolean {
|
||||
val returnType = descriptor.returnType
|
||||
return returnType != null && KotlinBuiltIns.isUnit(returnType)
|
||||
}
|
||||
|
||||
private fun getJVMFunctionName(functionDescriptor: FunctionDescriptor): String {
|
||||
return DescriptorUtils.getJvmName(functionDescriptor) ?: functionDescriptor.name.asString()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports a main function with incorrect return type (should be <b>Unit</b>).
|
||||
</body>
|
||||
</html>
|
||||
@@ -2783,6 +2783,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2783,6 +2783,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2783,6 +2783,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2784,6 +2784,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2784,6 +2784,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2785,6 +2785,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection"
|
||||
displayName="Main function should return Unit"
|
||||
groupPath="Kotlin"
|
||||
groupName="Probable bugs"
|
||||
enabledByDefault="true"
|
||||
level="WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix
|
||||
import com.intellij.codeInspection.ProblemDescriptor
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiElementVisitor
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.MainFunctionDetector
|
||||
import org.jetbrains.kotlin.idea.core.setType
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.namedFunctionVisitor
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class MainFunctionReturnUnitInspection : AbstractKotlinInspection() {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
|
||||
return namedFunctionVisitor(fun(function: KtNamedFunction) {
|
||||
if (function.name != "main") return
|
||||
|
||||
val descriptor = function.descriptor as? FunctionDescriptor ?: return
|
||||
if (!MainFunctionDetector.isMain(descriptor, checkReturnType = false)) return
|
||||
if (MainFunctionDetector.isMainReturnType(descriptor)) return
|
||||
|
||||
val funKeyword = function.funKeyword ?: return
|
||||
val valueParameterList = function.valueParameterList ?: return
|
||||
val typeReference = function.typeReference
|
||||
val start = function.startOffset
|
||||
val highlightRange = TextRange(
|
||||
funKeyword.startOffset - start,
|
||||
if (typeReference != null) typeReference.endOffset - start else valueParameterList.endOffset - start
|
||||
)
|
||||
|
||||
holder.registerProblem(
|
||||
holder.manager.createProblemDescriptor(
|
||||
function,
|
||||
highlightRange,
|
||||
"main should return Unit",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
isOnTheFly,
|
||||
ChangeMainFunctionReturnTypeToUnitFix(function.typeReference != null)
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private class ChangeMainFunctionReturnTypeToUnitFix(private val hasExplicitReturnType: Boolean) : LocalQuickFix {
|
||||
override fun getName() = if (hasExplicitReturnType) "Change return type to Unit" else "Add explicit Unit return type"
|
||||
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val function = descriptor.psiElement as? KtNamedFunction ?: return
|
||||
function.setType(KotlinBuiltIns.FQ_NAMES.unit.asString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.MainFunctionReturnUnitInspection
|
||||
@@ -0,0 +1,2 @@
|
||||
// PROBLEM: none
|
||||
<caret>fun test(args: Array<String>): Int = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// FIX: Add explicit Unit return type
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>) = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// FIX: Add explicit Unit return type
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>): Unit = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// FIX: Change return type to Unit
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>): Int = 1
|
||||
@@ -0,0 +1,3 @@
|
||||
// FIX: Change return type to Unit
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>): Unit = 1
|
||||
@@ -0,0 +1,4 @@
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>): Int {
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// DISABLE-ERRORS
|
||||
<caret>fun main(args: Array<String>): Unit {
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// PROBLEM: none
|
||||
<caret>fun main(args: Array<String>) {}
|
||||
+39
@@ -2320,6 +2320,45 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class MainFunctionReturnUnit extends AbstractLocalInspectionTest {
|
||||
public void testAllFilesPresentInMainFunctionReturnUnit() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/mainFunctionReturnUnit"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("notMain.kt")
|
||||
public void testNotMain() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit/notMain.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notUnit1.kt")
|
||||
public void testNotUnit1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit/notUnit1.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notUnit2.kt")
|
||||
public void testNotUnit2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit/notUnit2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("notUnit3.kt")
|
||||
public void testNotUnit3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit/notUnit3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unit.kt")
|
||||
public void testUnit() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/mainFunctionReturnUnit/unit.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/mayBeConstant")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user