KTIJ-22971 [kotlin] Adjust the applicability range for LabeledReturnAsLastExpressionInLambdaInspection

Now the quick fix would only be applied on the `return@foo`
range, and would not be available on the returned expression

GitOrigin-RevId: a0e6756cdc12543602da11b0d7418c7659fd1f16
This commit is contained in:
Roman Golyshev
2024-03-12 21:40:56 +01:00
committed by intellij-monorepo-bot
parent fbf7da2525
commit bbee37c964
12 changed files with 57 additions and 8 deletions

View File

@@ -12,7 +12,7 @@ import org.jetbrains.kotlin.idea.base.psi.getParentLambdaLabelName
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.inspections.AbstractKotlinApplicableInspection
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.KotlinApplicabilityRange
import org.jetbrains.kotlin.idea.codeinsights.impl.base.applicators.ApplicabilityRanges
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.applicabilityRange
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -51,7 +51,12 @@ internal class LabeledReturnAsLastExpressionInLambdaInspection : AbstractKotlinA
return true
}
override fun getApplicabilityRange(): KotlinApplicabilityRange<KtReturnExpression> = ApplicabilityRanges.SELF
override fun getApplicabilityRange(): KotlinApplicabilityRange<KtReturnExpression> = applicabilityRange { returnExpression ->
val keywordRange = returnExpression.returnKeyword.textRangeInParent
val labelRange = returnExpression.labeledExpression?.textRangeInParent
if (labelRange != null) keywordRange.union(labelRange) else null
}
override fun apply(
element: KtReturnExpression,

View File

@@ -523,6 +523,16 @@ public abstract class SharedK1LocalInspectionTestGenerated extends AbstractShare
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("inLabelRange.kt")
public void testInLabelRange() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/inLabelRange.kt");
}
@TestMetadata("inReturnedExpressionRange.kt")
public void testInReturnedExpressionRange() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/inReturnedExpressionRange.kt");
}
@TestMetadata("labeledLambda.kt")
public void testLabeledLambda() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/labeledLambda.kt");

View File

@@ -523,6 +523,16 @@ public abstract class SharedK2LocalInspectionTestGenerated extends AbstractShare
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("inLabelRange.kt")
public void testInLabelRange() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/inLabelRange.kt");
}
@TestMetadata("inReturnedExpressionRange.kt")
public void testInReturnedExpressionRange() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/inReturnedExpressionRange.kt");
}
@TestMetadata("labeledLambda.kt")
public void testLabeledLambda() throws Exception {
runTest("../testData/inspectionsLocal/labeledReturnAsLastExpressionInLambda/labeledLambda.kt");

View File

@@ -0,0 +1,8 @@
// WITH_STDLIB
// INTENTION_TEXT: "Remove return@find"
fun foo() {
listOf(1,2,3).find {
return@<caret>find true
}
}

View File

@@ -0,0 +1,8 @@
// WITH_STDLIB
// INTENTION_TEXT: "Remove return@find"
fun foo() {
listOf(1,2,3).find {
true
}
}

View File

@@ -0,0 +1,8 @@
// WITH_STDLIB
// PROBLEM: none
fun foo() {
listOf(1,2,3).find {
return@find <caret>true
}
}

View File

@@ -3,6 +3,6 @@
fun foo() {
listOf(1,2,3).find label@{
return@label <caret>true
<caret>return@label true
}
}

View File

@@ -3,7 +3,7 @@
fun foo() {
listOf(1,2,3).find {
if (it > 0) {
return@find <caret>true
<caret>return@find true
} else {
false
}

View File

@@ -3,6 +3,6 @@
fun foo() {
listOf(1,2,3).find {
return@find <caret>true
<caret>return@find true
}
}

View File

@@ -2,5 +2,5 @@
// PROBLEM: none
fun foo(): Boolean {
return@foo <caret>true
<caret>return@foo true
}

View File

@@ -3,7 +3,7 @@
fun foo(): Boolean {
listOf(1,2,3).find {
return <caret>true
<caret>return true
}
return false
}

View File

@@ -4,7 +4,7 @@
fun foo() {
listOf(1,2,3).forEach {
listOf(1,2,3).find {
return@forEach<caret>
<caret>return@forEach
}
}
}