Don't suggest "Remove variable" for property with function initializer

So #KT-23752 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-05-08 05:15:44 +03:00
committed by Mikhail Glukhikh
parent b8f8802cff
commit e770aed084
9 changed files with 62 additions and 11 deletions
@@ -58,17 +58,7 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
val initializer = (element as? KtDeclarationWithInitializer)?.initializer
if (initializer is KtLambdaExpression || initializer is KtNamedFunction) {
val functionType = element.typeReference?.typeElement as? KtFunctionType
if (functionType?.parameters?.isNotEmpty() == true) {
val valueParameters = when (initializer) {
is KtLambdaExpression -> initializer.valueParameters
is KtNamedFunction -> initializer.valueParameters
else -> emptyList()
}
if (valueParameters.isEmpty() || valueParameters.any { it.typeReference == null }) return null
}
}
if (!redundantTypeSpecification(element, initializer)) return null
return when {
initializer != null -> TextRange(element.startOffset, initializer.startOffset - 1)
@@ -77,6 +67,19 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
else -> null
}
}
fun redundantTypeSpecification(element: KtCallableDeclaration, initializer: KtExpression?): Boolean {
if (initializer == null) return true
if (initializer !is KtLambdaExpression && initializer !is KtNamedFunction) return true
val functionType = element.typeReference?.typeElement as? KtFunctionType ?: return true
if (functionType.parameters.isEmpty()) return true
val valueParameters = when (initializer) {
is KtLambdaExpression -> initializer.valueParameters
is KtNamedFunction -> initializer.valueParameters
else -> emptyList()
}
return valueParameters.isNotEmpty() && valueParameters.none { it.typeReference == null }
}
}
}
@@ -20,6 +20,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -60,6 +61,7 @@ open class RemovePsiElementSimpleFix(element: PsiElement, private val text: Stri
object RemoveVariableFactory : KotlinSingleIntentionActionFactory() {
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtProperty>() ?: return null
if (!RemoveExplicitTypeIntention.redundantTypeSpecification(expression, expression.initializer)) return null
return object : RemovePsiElementSimpleFix(expression, "Remove variable '${expression.name}'") {
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val initializer = expression.initializer
@@ -0,0 +1,5 @@
// "Remove variable 'a'" "false"
// ACTION: Split property declaration
fun test() {
val <caret>a: (String) -> Unit = fun(s) { s + s }
}
@@ -0,0 +1,4 @@
// "Remove variable 'a'" "true"
fun test() {
val <caret>a: (String) -> Unit = fun(s: String) { s + s }
}
@@ -0,0 +1,4 @@
// "Remove variable 'a'" "true"
fun test() {
fun(s: String) { s + s }
}
@@ -0,0 +1,5 @@
// "Remove variable 'a'" "false"
// ACTION: Split property declaration
fun test() {
val <caret>a: (String) -> Unit = { s -> s + s }
}
@@ -0,0 +1,4 @@
// "Remove variable 'a'" "true"
fun test() {
val <caret>a: (String) -> Unit = { s: String -> s + s }
}
@@ -0,0 +1,4 @@
// "Remove variable 'a'" "true"
fun test() {
{ s: String -> s + s }
}
@@ -11910,6 +11910,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/variables"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("unusedVariableWithAnonymousFunctionInitialize1.kt")
public void testUnusedVariableWithAnonymousFunctionInitialize1() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithAnonymousFunctionInitialize1.kt");
}
@TestMetadata("unusedVariableWithAnonymousFunctionInitialize2.kt")
public void testUnusedVariableWithAnonymousFunctionInitialize2() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithAnonymousFunctionInitialize2.kt");
}
@TestMetadata("unusedVariableWithConstantInitializer.kt")
public void testUnusedVariableWithConstantInitializer() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithConstantInitializer.kt");
@@ -11925,6 +11935,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/variables/unusedVariableWithInitializerAndComment.kt");
}
@TestMetadata("unusedVariableWithLambdaInitializer1.kt")
public void testUnusedVariableWithLambdaInitializer1() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithLambdaInitializer1.kt");
}
@TestMetadata("unusedVariableWithLambdaInitializer2.kt")
public void testUnusedVariableWithLambdaInitializer2() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithLambdaInitializer2.kt");
}
@TestMetadata("unusedVariableWithNullInitializer.kt")
public void testUnusedVariableWithNullInitializer() throws Exception {
runTest("idea/testData/quickfix/variables/unusedVariableWithNullInitializer.kt");