From c8c485d27e23dab061b3bd7179ac7bd7a60bbc9c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 6 Dec 2018 16:15:35 +0300 Subject: [PATCH] Redundant arrow: support removing of 'it ->' #KT-28631 Fixed --- .../idea/inspections/RedundantLambdaArrowInspection.kt | 4 +++- .../inspectionsLocal/redundantLambdaArrow/it.kt | 10 ++++++++++ .../inspectionsLocal/redundantLambdaArrow/it.kt.after | 10 ++++++++++ .../redundantLambdaArrow/underscore.kt | 2 +- .../redundantLambdaArrow/underscore.kt.after | 2 +- .../idea/inspections/LocalInspectionTestGenerated.java | 5 +++++ 6 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt create mode 100644 idea/testData/inspectionsLocal/redundantLambdaArrow/it.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 a6da84cf852..8712fea2408 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantLambdaArrowInspection.kt @@ -30,7 +30,9 @@ class RedundantLambdaArrowInspection : AbstractKotlinInspection() { val arrow = functionLiteral.arrow ?: return@lambdaExpressionVisitor val parameters = functionLiteral.valueParameters val singleParameter = parameters.singleOrNull() - if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true) return@lambdaExpressionVisitor + if (parameters.isNotEmpty() && singleParameter?.isSingleUnderscore != true && singleParameter?.name != "it") { + return@lambdaExpressionVisitor + } if (lambdaExpression.getStrictParentOfType()?.expression == lambdaExpression) return@lambdaExpressionVisitor if (lambdaExpression.getStrictParentOfType()?.let { diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt new file mode 100644 index 00000000000..d572186d082 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt @@ -0,0 +1,10 @@ +fun foo(f: (String) -> Unit) {} + +fun print(s: String) {} + +fun bar() { + foo { it -> + print(it) + print(it) + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt.after new file mode 100644 index 00000000000..122075de34f --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt.after @@ -0,0 +1,10 @@ +fun foo(f: (String) -> Unit) {} + +fun print(s: String) {} + +fun bar() { + foo { + print(it) + print(it) + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt index dfaa06f2fae..c9d732d1160 100644 --- a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt @@ -1,4 +1,4 @@ -fun foo(f: () -> Unit) {} +fun foo(f: (String) -> Unit) {} fun bar() { foo { _ -> } diff --git a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after index 2f5053d30e2..38c94b39254 100644 --- a/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after +++ b/idea/testData/inspectionsLocal/redundantLambdaArrow/underscore.kt.after @@ -1,4 +1,4 @@ -fun foo(f: () -> Unit) {} +fun foo(f: (String) -> Unit) {} fun bar() { foo { } diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 7f7a95f0773..92a6630d32a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -4482,6 +4482,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/inIfWhenEntry2.kt"); } + @TestMetadata("it.kt") + public void testIt() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/it.kt"); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { runTest("idea/testData/inspectionsLocal/redundantLambdaArrow/simple.kt");