From 2010b1756e0a837dabf352e679a88479f2da4702 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 30 Dec 2018 13:45:00 +0900 Subject: [PATCH] Remove unnecessary parentheses: don't suggest when parentheses are for lambda --- .../src/org/jetbrains/kotlin/psi/KtPsiUtil.java | 2 +- .../removeUnnecessaryParentheses/lambda.kt | 8 ++++++++ .../removeUnnecessaryParentheses/lambda2.kt | 8 ++++++++ .../removeUnnecessaryParentheses/lambda3.kt | 8 ++++++++ .../idea/intentions/IntentionTestGenerated.java | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/lambda.kt create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/lambda2.kt create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/lambda3.kt diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index fe5bbc31771..da1b44f87c2 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -448,7 +448,7 @@ public class KtPsiUtil { } } - if (innerExpression instanceof KtLambdaExpression) { + if (innerExpression instanceof KtLambdaExpression || innerExpression.getFirstChild() instanceof KtLambdaExpression) { PsiElement prevSibling = PsiTreeUtil.skipWhitespacesAndCommentsBackward(currentInner); if (prevSibling != null && prevSibling.getText().endsWith(KtTokens.RPAR.getValue())) return true; } diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/lambda.kt b/idea/testData/intentions/removeUnnecessaryParentheses/lambda.kt new file mode 100644 index 00000000000..de18ea34869 --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/lambda.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false + +fun main() { + foo() + ({ foo() } ) +} + +fun foo() {} diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/lambda2.kt b/idea/testData/intentions/removeUnnecessaryParentheses/lambda2.kt new file mode 100644 index 00000000000..3a7659c8c7d --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/lambda2.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false + +fun main() { + foo() + ({ foo() } as? () -> Unit) +} + +fun foo() {} diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/lambda3.kt b/idea/testData/intentions/removeUnnecessaryParentheses/lambda3.kt new file mode 100644 index 00000000000..5b23af4e675 --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/lambda3.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false + +fun main() { + foo() + ({ foo() }.invoke()) +} + +fun foo() {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 58dd6248408..25f1cea17b9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -14274,6 +14274,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt"); } + @TestMetadata("lambda.kt") + public void testLambda() throws Exception { + runTest("idea/testData/intentions/removeUnnecessaryParentheses/lambda.kt"); + } + + @TestMetadata("lambda2.kt") + public void testLambda2() throws Exception { + runTest("idea/testData/intentions/removeUnnecessaryParentheses/lambda2.kt"); + } + + @TestMetadata("lambda3.kt") + public void testLambda3() throws Exception { + runTest("idea/testData/intentions/removeUnnecessaryParentheses/lambda3.kt"); + } + @TestMetadata("necessaryParentheses1.kt") public void testNecessaryParentheses1() throws Exception { runTest("idea/testData/intentions/removeUnnecessaryParentheses/necessaryParentheses1.kt");