From d59276671f98d7d19a6d0709e8eeb569d48cbe3e Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 31 Oct 2017 16:38:55 +0300 Subject: [PATCH] Related to KT-20631: don't report redundant blank Unit in lambda --- .../RedundantUnitExpressionInspection.kt | 4 ++++ .../redundantUnitExpression/lambda.kt | 12 ++++++++++++ .../redundantUnitExpression/redundant2.kt | 5 ----- .../redundant2.kt.after | 4 ---- .../redundantUnitExpression/redundant3.kt | 6 ------ .../redundant3.kt.after | 5 ----- .../LocalInspectionTestGenerated.java | 18 ++++++------------ 7 files changed, 22 insertions(+), 32 deletions(-) create mode 100644 idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt delete mode 100644 idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt delete mode 100644 idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt.after delete mode 100644 idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt delete mode 100644 idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitExpressionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitExpressionInspection.kt index 20f8bdb53a0..b7f2d3e8c5a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitExpressionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitExpressionInspection.kt @@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor { @@ -36,6 +37,9 @@ class RedundantUnitExpressionInspection : AbstractKotlinInspection(), CleanupLoc val parent = expression.parent if (parent !is KtReturnExpression && parent !is KtBlockExpression) return + // Do not report just 'Unit' in function literals (return@label Unit is OK even in literals) + if (parent is KtBlockExpression && parent.getParentOfType(strict = true) != null) return + holder.registerProblem(expression, "Redundant 'Unit'", ProblemHighlightType.LIKE_UNUSED_SYMBOL, diff --git a/idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt b/idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt new file mode 100644 index 00000000000..bc66cd80150 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt @@ -0,0 +1,12 @@ +// PROBLEM: none + +fun run(f: () -> T) = f() + +fun foo(s: String) = s + +fun test() { + run { + foo("Hello") + Unit + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt b/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt deleted file mode 100644 index db80df532e5..00000000000 --- a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt +++ /dev/null @@ -1,5 +0,0 @@ -fun test() { - val f: () -> Unit = { - Unit - } -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt.after b/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt.after deleted file mode 100644 index 20ea98bdc43..00000000000 --- a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt.after +++ /dev/null @@ -1,4 +0,0 @@ -fun test() { - val f: () -> Unit = { - } -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt b/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt deleted file mode 100644 index 385c99b2597..00000000000 --- a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt +++ /dev/null @@ -1,6 +0,0 @@ -fun test() { - val f: () -> Unit = { - Unit - Unit - } -} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt.after b/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt.after deleted file mode 100644 index 4e97610283e..00000000000 --- a/idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt.after +++ /dev/null @@ -1,5 +0,0 @@ -fun test() { - val f: () -> Unit = { - Unit - } -} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index a694245c1b4..853c539224a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1637,6 +1637,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/redundantUnitExpression"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("lambda.kt") + public void testLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/lambda.kt"); + doTest(fileName); + } + @TestMetadata("notRedundant1.kt") public void testNotRedundant1() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/notRedundant1.kt"); @@ -1661,18 +1667,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } - @TestMetadata("redundant2.kt") - public void testRedundant2() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant2.kt"); - doTest(fileName); - } - - @TestMetadata("redundant3.kt") - public void testRedundant3() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant3.kt"); - doTest(fileName); - } - @TestMetadata("redundant4.kt") public void testRedundant4() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/redundantUnitExpression/redundant4.kt");