Add inspection for unlabeled return inside lambda #KT-26511 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-08-31 12:43:06 +03:00
committed by Mikhail Glukhikh
parent ba32ed7404
commit 6cd13341ee
14 changed files with 148 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports unlabeled <b>return</b> inside lambda.
</body>
</html>
+9
View File
@@ -3066,6 +3066,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3064,6 +3064,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3065,6 +3065,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3066,6 +3066,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3064,6 +3064,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3064,6 +3064,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
+9
View File
@@ -3066,6 +3066,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection"
displayName="Unlabeled return inside lambda"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.returnExpressionVisitor
class UnlabeledReturnInsideLambdaInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
returnExpressionVisitor(fun(returnExpression: KtReturnExpression) {
if (returnExpression.labelQualifier != null) return
if (returnExpression.getParentOfType<KtLambdaExpression>(true, KtNamedFunction::class.java) == null) return
holder.registerProblem(
returnExpression,
"Unlabeled return inside lambda",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
)
})
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.UnlabeledReturnInsideLambdaInspection
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
listOf(1).forEach {
if (it == 10) <caret>return@forEach
}
}
@@ -0,0 +1,7 @@
// FIX: none
// WITH_RUNTIME
fun test() {
listOf(1).forEach {
if (it == 10) <caret>return
}
}
@@ -0,0 +1,9 @@
// PROBLEM: none
// WITH_RUNTIME
fun test() {
listOf(1).forEach {
fun foo() {
if (it == 10) <caret>return
}
}
}
@@ -5999,6 +5999,34 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class UnlabeledReturnInsideLambda extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInUnlabeledReturnInsideLambda() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("labeledReturn.kt")
public void testLabeledReturn() throws Exception {
runTest("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda/labeledReturn.kt");
}
@TestMetadata("return.kt")
public void testReturn() throws Exception {
runTest("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda/return.kt");
}
@TestMetadata("returnInFunction.kt")
public void testReturnInFunction() throws Exception {
runTest("idea/testData/inspectionsLocal/unlabeledReturnInsideLambda/returnInFunction.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/unnecessaryVariable")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)