From 9ce40cd17bfd7fb06b68f7be14d86e4bbb325467 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Wed, 5 Feb 2014 16:33:34 +0200 Subject: [PATCH] Fix unnecessary parenthesis around 'this' in refactorings Operator priority calculation for 'this' expressions was incorrect in JetPsiUtil.getPriority(): it would return a very low value for a JetThisExpression, causing the IDE to report that parenthesis are necessary in an expression like '(this)[1]'. Fix this by making JetThisExpression not implement JetStatementExpression by removing the implements clause from JetLabelQualifiedInstanceExpression and adding it to all other subclasses of JetLabelQualifiedInstanceExpression. Fixes KT-4513. --- .../jet/lang/psi/JetLabelQualifiedInstanceExpression.java | 3 +-- .../src/org/jetbrains/jet/lang/psi/JetSuperExpression.java | 2 +- .../removeUnnecessaryParentheses/unnecessaryParentheses8.kt | 6 ++++++ .../unnecessaryParentheses8.kt.after | 6 ++++++ .../plugin/intentions/CodeTransformationTestGenerated.java | 5 +++++ 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt create mode 100644 idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt.after diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelQualifiedInstanceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelQualifiedInstanceExpression.java index ccba84517a2..6f10bc322d0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelQualifiedInstanceExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelQualifiedInstanceExpression.java @@ -20,8 +20,7 @@ import com.intellij.lang.ASTNode; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetNodeTypes; -public abstract class JetLabelQualifiedInstanceExpression extends JetLabelQualifiedExpression - implements JetStatementExpression { +public abstract class JetLabelQualifiedInstanceExpression extends JetLabelQualifiedExpression { public JetLabelQualifiedInstanceExpression(@NotNull ASTNode node) { super(node); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSuperExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSuperExpression.java index 020f2b12c69..35821b9a70d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSuperExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSuperExpression.java @@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; -public class JetSuperExpression extends JetLabelQualifiedInstanceExpression { +public class JetSuperExpression extends JetLabelQualifiedInstanceExpression implements JetStatementExpression { public JetSuperExpression(@NotNull ASTNode node) { super(node); diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt new file mode 100644 index 00000000000..e4ecc0bcfe9 --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt @@ -0,0 +1,6 @@ +class C() { + fun get(i: Int) = i + 1 + + // KT-4513: Unnecessary parenthesis around 'this' + val foo = (this)[41] +} diff --git a/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt.after b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt.after new file mode 100644 index 00000000000..2b597c6f0f3 --- /dev/null +++ b/idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt.after @@ -0,0 +1,6 @@ +class C() { + fun get(i: Int) = i + 1 + + // KT-4513: Unnecessary parenthesis around 'this' + val foo = this[41] +} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 35de3fd4b5d..1aff6c9a6fa 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -1176,6 +1176,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT doTestRemoveUnnecessaryParentheses("idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses7.kt"); } + @TestMetadata("unnecessaryParentheses8.kt") + public void testUnnecessaryParentheses8() throws Exception { + doTestRemoveUnnecessaryParentheses("idea/testData/intentions/removeUnnecessaryParentheses/unnecessaryParentheses8.kt"); + } + } public static Test suite() {