From 4ae837e6691f2d0965cbcea8a2d63893f03e302a Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 8 Nov 2018 12:05:46 +0900 Subject: [PATCH] Redundant lambda arrow: fix false positive in 'when/if' without block #KT-28047 Fixed --- .../RedundantLambdaArrowInspection.kt | 9 ++++++ .../redundantLambdaArrow/inIfElse.kt | 4 +++ .../redundantLambdaArrow/inIfElse2.kt | 7 +++++ .../redundantLambdaArrow/inIfElse2.kt.after | 7 +++++ .../redundantLambdaArrow/inIfThen.kt | 4 +++ .../redundantLambdaArrow/inIfThen2.kt | 7 +++++ .../redundantLambdaArrow/inIfThen2.kt.after | 7 +++++ .../redundantLambdaArrow/inIfWhenEntry.kt | 7 +++++ .../redundantLambdaArrow/inIfWhenEntry2.kt | 10 +++++++ .../inIfWhenEntry2.kt.after | 10 +++++++ .../LocalInspectionTestGenerated.java | 30 +++++++++++++++++++ 11 files changed, 102 insertions(+) create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt.after create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt.after create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt index d449894174e..a6da84cf852 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -13,9 +13,13 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.psi.KtContainerNodeForControlStructureBody import org.jetbrains.kotlin.psi.KtFunctionLiteral +import org.jetbrains.kotlin.psi.KtWhenEntry import org.jetbrains.kotlin.psi.lambdaExpressionVisitor import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore @@ -28,6 +32,11 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() { val singleParameter = parameters.singleOrNull() if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true) return@lambdaExpressionVisitor + if (lambdaExpression.getStrictParentOfType()?.expression == lambdaExpression) return@lambdaExpressionVisitor + if (lambdaExpression.getStrictParentOfType()?.let { + it.node.elementType in listOf(KtNodeTypes.THEN, KtNodeTypes.ELSE) && it.expression == lambdaExpression + } == true) return@lambdaExpressionVisitor + val startOffset = functionLiteral.startOffset holder.registerProblem( holder.manager.createProblemDescriptor( diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt new file mode 100644 index 00000000000..84e7a4a5114 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt @@ -0,0 +1,4 @@ +// PROBLEM: none +fun test(): (Int) -> Int { + return if (true) { _ -> 42 } else { _ -> 42 } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt new file mode 100644 index 00000000000..56c29d1fb02 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt @@ -0,0 +1,7 @@ +fun test(): (Int) -> Int { + return if (true) { + { _ -> 42 } + } else { + { _ -> 42 } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt.after new file mode 100644 index 00000000000..6e725e6f8ee --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt.after @@ -0,0 +1,7 @@ +fun test(): (Int) -> Int { + return if (true) { + { _ -> 42 } + } else { + { 42 } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt new file mode 100644 index 00000000000..aeaceb65184 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt @@ -0,0 +1,4 @@ +// PROBLEM: none +fun test(): (Int) -> Int { + return if (true) { _ -> 42 } else { _ -> 42 } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt new file mode 100644 index 00000000000..2b961015d95 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt @@ -0,0 +1,7 @@ +fun test(): (Int) -> Int { + return if (true) { + { _ -> 42 } + } else { + { _ -> 42 } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt.after new file mode 100644 index 00000000000..08d67913e8a --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt.after @@ -0,0 +1,7 @@ +fun test(): (Int) -> Int { + return if (true) { + { 42 } + } else { + { _ -> 42 } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt new file mode 100644 index 00000000000..bf18978960b --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +fun test(): (Int) -> Int { + return when { + true -> { _ -> 42 } + else -> { _ -> 42 } + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt new file mode 100644 index 00000000000..1d7cc97a1ee --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt @@ -0,0 +1,10 @@ +fun test(): (Int) -> Int { + return when { + true -> { + { _ -> 42 } + } + else -> { + { _ -> 42 } + } + } +} diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt.after new file mode 100644 index 00000000000..bde4a8b3267 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt.after @@ -0,0 +1,10 @@ +fun test(): (Int) -> Int { + return when { + true -> { + { 42 } + } + else -> { + { _ -> 42 } + } + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index b52e0d0f978..13642ed658d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -4233,6 +4233,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/hasArguments.kt"); } + @TestMetadata("inIfElse.kt") + public void testInIfElse() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse.kt"); + } + + @TestMetadata("inIfElse2.kt") + public void testInIfElse2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfElse2.kt"); + } + + @TestMetadata("inIfThen.kt") + public void testInIfThen() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen.kt"); + } + + @TestMetadata("inIfThen2.kt") + public void testInIfThen2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfThen2.kt"); + } + + @TestMetadata("inIfWhenEntry.kt") + public void testInIfWhenEntry() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry.kt"); + } + + @TestMetadata("inIfWhenEntry2.kt") + public void testInIfWhenEntry2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt"); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt");