detect valid extension main functions (#1227)

Fixes #KT-18083
This commit is contained in:
Kirill Rakhman
2017-08-07 11:39:34 +02:00
committed by Dmitry Jemerov
parent 4c00119f08
commit 2536615e0e
2 changed files with 18 additions and 6 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2017 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -52,7 +52,15 @@ class MainFunctionDetector {
return false return false
} }
if (function.valueParameters.size != 1 || !function.typeParameters.isEmpty()) {
var parametersCount = function.valueParameters.size
if (function.receiverTypeReference != null) parametersCount++
if (parametersCount != 1) {
return false
}
if (!function.typeParameters.isEmpty()) {
return false return false
} }
@@ -98,11 +106,12 @@ class MainFunctionDetector {
return false return false
} }
val parameters = descriptor.valueParameters val parameters = descriptor.valueParameters.mapTo(mutableListOf()) { it.type }
descriptor.extensionReceiverParameter?.type?.let {parameters += it}
if (parameters.size != 1 || !descriptor.typeParameters.isEmpty()) return false if (parameters.size != 1 || !descriptor.typeParameters.isEmpty()) return false
val parameter = parameters[0] val parameterType = parameters[0]
val parameterType = parameter.type
if (!KotlinBuiltIns.isArray(parameterType)) return false if (!KotlinBuiltIns.isArray(parameterType)) return false
val typeArguments = parameterType.arguments val typeArguments = parameterType.arguments
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2016 JetBrains s.r.o. * Copyright 2010-2017 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.MainFunctionDetector
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.isOverridable import org.jetbrains.kotlin.idea.core.isOverridable
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureConfiguration
@@ -54,6 +55,8 @@ class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
val callable = callableDeclaration.descriptor val callable = callableDeclaration.descriptor
if (callable != null && MainFunctionDetector.isMain(callable)) return
var used = false var used = false
callableDeclaration.acceptChildren(object : KtVisitorVoid() { callableDeclaration.acceptChildren(object : KtVisitorVoid() {
override fun visitKtElement(element: KtElement) { override fun visitKtElement(element: KtElement) {