From e8b95b971d75bbda8f8a67b79f9125fe124ac57b Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 18 Sep 2017 16:43:36 +0300 Subject: [PATCH] Make parentheses necessary for '(x op return y) op z' #KT-16808 Fixed --- .../org/jetbrains/kotlin/psi/KtPsiUtil.java | 18 +++++++++++++----- .../removeUnnecessaryParentheses/elvisRhs.kt | 5 +++++ .../elvisRhsEmptyReturn.kt | 7 +++++++ .../intentions/IntentionTestGenerated.java | 12 ++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/elvisRhs.kt create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index 3409f30fc41..f39657bde07 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -530,11 +530,19 @@ public class KtPsiUtil { return false; } - // '(x operator y)' case - if (innerExpression instanceof KtBinaryExpression && - innerOperation != KtTokens.ELVIS && - isKeepBinaryExpressionParenthesized((KtBinaryExpression) innerExpression)) { - return true; + if (innerExpression instanceof KtBinaryExpression) { + // '(x operator return [...]) operator ...' case + if (parentElement instanceof KtBinaryExpression) { + KtBinaryExpression innerBinary = (KtBinaryExpression) innerExpression; + if (innerBinary.getRight() instanceof KtReturnExpression) { + return true; + } + } + // '(x operator y)' case + if (innerOperation != KtTokens.ELVIS && + isKeepBinaryExpressionParenthesized((KtBinaryExpression) innerExpression)) { + return true; + } } int innerPriority = getPriority(innerExpression); diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhs.kt b/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhs.kt new file mode 100644 index 00000000000..de82156154f --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhs.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +fun foo(): Boolean { + return ("" ?: return false) in listOf("") +} \ No newline at end of file diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt b/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt new file mode 100644 index 00000000000..0968e2bc96e --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +fun foo() { + bar(("" ?: return) in listOf("")) +} + +fun bar(arg: Boolean) {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index f004cf2f427..ffbee40d147 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13412,6 +13412,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("elvisRhs.kt") + public void testElvisRhs() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryParentheses/elvisRhs.kt"); + doTest(fileName); + } + + @TestMetadata("elvisRhsEmptyReturn.kt") + public void testElvisRhsEmptyReturn() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryParentheses/elvisRhsEmptyReturn.kt"); + doTest(fileName); + } + @TestMetadata("necessaryParentheses1.kt") public void testNecessaryParentheses1() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeUnnecessaryParentheses/necessaryParentheses1.kt");