Create WarningOnMainUnusedParameterMigrationInspection
#KT-36256 Fixed
This commit is contained in:
@@ -3179,6 +3179,16 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.migration.WarningOnMainUnusedParameterMigrationInspection"
|
||||
displayName="Unused `args` on `main` since 1.4"
|
||||
groupPath="Kotlin"
|
||||
groupName="Migration"
|
||||
enabledByDefault="false"
|
||||
cleanupTool="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.migration.ObsoleteExperimentalCoroutinesInspection"
|
||||
displayName="Experimental coroutines usages are deprecated since 1.3"
|
||||
groupPath="Kotlin"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Since Kotlin 1.4, a main function with an unused parameter has a compiler warning <code>UNUSED_PARAMETER</code>.
|
||||
</body>
|
||||
</html>
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.migration
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInspection.CleanupLocalInspectionTool
|
||||
import org.jetbrains.kotlin.config.LanguageVersion
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.MainFunctionDetector
|
||||
import org.jetbrains.kotlin.idea.configuration.MigrationInfo
|
||||
import org.jetbrains.kotlin.idea.configuration.isLanguageVersionUpdate
|
||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveUnusedFunctionParameterFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.migration.MigrationFix
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
|
||||
|
||||
class WarningOnMainUnusedParameterMigrationInspection :
|
||||
AbstractDiagnosticBasedMigrationInspection<KtParameter>(
|
||||
Errors.UNUSED_PARAMETER,
|
||||
KtParameter::class.java,
|
||||
::createIntentionActionForMainFunction,
|
||||
),
|
||||
MigrationFix,
|
||||
CleanupLocalInspectionTool {
|
||||
override fun isApplicable(migrationInfo: MigrationInfo): Boolean {
|
||||
return migrationInfo.isLanguageVersionUpdate(LanguageVersion.KOTLIN_1_3, LanguageVersion.KOTLIN_1_4)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createIntentionActionForMainFunction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val parameter = diagnostic.psiElement as? KtParameter ?: return null
|
||||
val ownerFunction = parameter.ownerFunction as? KtNamedFunction ?: return null
|
||||
val mainFunctionDetector = MainFunctionDetector(parameter.languageVersionSettings) { it.descriptor as? FunctionDescriptor }
|
||||
if (!mainFunctionDetector.isMain(ownerFunction)) return null
|
||||
return RemoveUnusedFunctionParameterFix(parameter, false)
|
||||
}
|
||||
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.quickfix
|
||||
@@ -30,7 +19,8 @@ import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixAction<KtParameter>(parameter) {
|
||||
class RemoveUnusedFunctionParameterFix(parameter: KtParameter, private val checkUsages: Boolean = true) :
|
||||
KotlinQuickFixAction<KtParameter>(parameter) {
|
||||
override fun getFamilyName() = ChangeFunctionSignatureFix.FAMILY_NAME
|
||||
|
||||
override fun getText() = element?.let { "Remove parameter '${it.name}'" } ?: ""
|
||||
@@ -40,6 +30,13 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val parameter = element ?: return
|
||||
val parameterList = parameter.parent as? KtParameterList ?: return
|
||||
if (!checkUsages) {
|
||||
runWriteAction {
|
||||
parameterList.removeParameter(parameter)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val parameterDescriptor = parameter.resolveToParameterDescriptorIfAny(BodyResolveMode.FULL) ?: return
|
||||
val parameterSize = parameterList.parameters.size
|
||||
val typeParameters = typeParameters(parameter.typeReference)
|
||||
@@ -81,7 +78,7 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
||||
if (typeReference == null) return emptyList()
|
||||
val parameterParent = typeReference.getParentOfTypesAndPredicate(
|
||||
true,
|
||||
KtNamedFunction::class.java, KtProperty::class.java, KtClass::class.java
|
||||
KtNamedFunction::class.java, KtProperty::class.java, KtClass::class.java,
|
||||
) { true }
|
||||
return typeReference.typeElement
|
||||
?.collectDescendantsOfType<KtNameReferenceExpression> { true }
|
||||
@@ -89,7 +86,7 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
|
||||
val typeParameter = it.reference?.resolve() as? KtTypeParameter ?: return@mapNotNull null
|
||||
val parent = typeParameter.getParentOfTypesAndPredicate(
|
||||
true,
|
||||
KtNamedFunction::class.java, KtProperty::class.java, KtClass::class.java
|
||||
KtNamedFunction::class.java, KtProperty::class.java, KtClass::class.java,
|
||||
) { true }
|
||||
if (parent == parameterParent) typeParameter else null
|
||||
} ?: emptyList()
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.migration.WarningOnMainUnusedParameterMigrationInspection
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// LANGUAGE_VERSION: 1.4
|
||||
// ERROR: Too many arguments for public fun main(): Unit defined in root package in file simple.kt
|
||||
|
||||
fun main(args<caret>: Array<String>) {
|
||||
}
|
||||
|
||||
fun b() {
|
||||
main(arrayOf("test"))
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// LANGUAGE_VERSION: 1.4
|
||||
// ERROR: Too many arguments for public fun main(): Unit defined in root package in file simple.kt
|
||||
|
||||
fun main() {
|
||||
}
|
||||
|
||||
fun b() {
|
||||
main(arrayOf("test"))
|
||||
}
|
||||
+18
@@ -13325,6 +13325,24 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/warningOnMainUnusedParameterMigration")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class WarningOnMainUnusedParameterMigration extends AbstractLocalInspectionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInWarningOnMainUnusedParameterMigration() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/warningOnMainUnusedParameterMigration"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/warningOnMainUnusedParameterMigration/simple.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/whenWithOnlyElse")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user