Do not suggest "Remove parameter" quick-fix on lambdas

Only "Rename to _" should work in the case.

The problem is that removing lambda's parameter may lead behaviour changes.
This quick-fix was not being invoked run before KT-14347 had been implemented

# Conflicts:
#	idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
This commit is contained in:
Denis Zharkov
2016-10-21 13:13:28 +03:00
parent 5e1b219784
commit 7d89869c02
4 changed files with 47 additions and 2 deletions
@@ -23,6 +23,8 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtParameter
class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixAction<KtParameter>(parameter) {
@@ -38,8 +40,12 @@ class RemoveUnusedFunctionParameterFix(parameter: KtParameter) : KotlinQuickFixA
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtParameter> {
return RemoveUnusedFunctionParameterFix(Errors.UNUSED_PARAMETER.cast(diagnostic).psiElement)
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtParameter>? {
val parameter = Errors.UNUSED_PARAMETER.cast(diagnostic).psiElement
val parameterOwner = parameter.parent.parent
if (parameterOwner is KtFunctionLiteral ||
(parameterOwner is KtNamedFunction && parameterOwner.name == null)) return null
return RemoveUnusedFunctionParameterFix(parameter)
}
}
}
@@ -0,0 +1,13 @@
// "Remove parameter 'x'" "false"
// ACTION: Add 'block =' to argument
// ACTION: Convert parameter to receiver
// ACTION: Rename to _
// ACTION: Specify return type explicitly
fun foo(block: (String, Int) -> Unit) {
block("", 1)
}
fun bar() {
foo(fun(x<caret>: String, y: Int) = Unit)
}
@@ -0,0 +1,14 @@
// "Remove parameter 'x'" "false"
// ACTION: Move lambda argument into parentheses
// ACTION: Remove explicit lambda parameter types (may break code)
// ACTION: Rename to _
fun foo(block: (String, Int) -> Unit) {
block("", 1)
}
fun bar() {
foo { x<caret>: String, y: Int ->
y.hashCode()
}
}
@@ -7439,6 +7439,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("functionExpressionParameterNoRemoveParameter.kt")
public void testFunctionExpressionParameterNoRemoveParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/functionExpressionParameterNoRemoveParameter.kt");
doTest(fileName);
}
@TestMetadata("lambdaDestructuring.kt")
public void testLambdaDestructuring() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/lambdaDestructuring.kt");
@@ -7451,6 +7457,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("lambdaParameterNoRemoveParameter.kt")
public void testLambdaParameterNoRemoveParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/lambdaParameterNoRemoveParameter.kt");
doTest(fileName);
}
@TestMetadata("noActionForCommonVal.kt")
public void testNoActionForCommonVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/noActionForCommonVal.kt");