Fix potential leak in "unused lambda expression body" fix

This commit is contained in:
Mikhail Glukhikh
2017-12-27 17:49:35 +03:00
parent 8e23ca597d
commit 2e71691ab2
6 changed files with 12 additions and 19 deletions
@@ -18,11 +18,13 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.LocalQuickFixOnPsiElement
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -63,12 +65,13 @@ class UnusedLambdaExpressionBodyInspection : AbstractKotlinInspection() {
private fun CallableDescriptor.returnsFunction() = returnType?.isFunctionType ?: false
class RemoveEqTokenFromFunctionDeclarationFix(val function: KtFunction) : LocalQuickFix {
override fun getName(): String = "Remove '=' token from function declaration"
class RemoveEqTokenFromFunctionDeclarationFix(function: KtFunction) : LocalQuickFixOnPsiElement(function) {
override fun getText(): String = "Remove '=' token from function declaration"
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
val function = startElement as? KtFunction ?: return
if (!FileModificationService.getInstance().preparePsiElementForWrite(function)) {
return
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.inspections.UnusedLambdaExpressionBodyInspection
@@ -3874,6 +3874,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/inEnumEntry.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/unusedLambdaExpressionBody/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/unusedReceiverParameter")
@@ -9021,21 +9021,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveEqTokenFromFunctionDeclaration extends AbstractQuickFixTest {
public void testAllFilesPresentInRemoveEqTokenFromFunctionDeclaration() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeEqTokenFromFunctionDeclaration/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/removeFinalUpperBound")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)