From e824cc5fa412f3e9c5b9acec767fa7a172a07571 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 21 Nov 2018 18:14:14 +0100 Subject: [PATCH] switch expression precedence --- .../intellij/psi/util/PsiPrecedenceUtil.java | 2 +- .../tree/java/ReplaceExpressionUtil.java | 18 ++---------------- .../parentheses/SwitchExpression.after.java | 8 ++++++++ .../igfixes/parentheses/SwitchExpression.java | 8 ++++++++ .../UnnecessaryParenthesesInspection.java | 10 ++++++++++ .../UnnecessaryParenthesesQuickFixTest.java | 18 +++--------------- .../UnnecessaryParenthesesInspectionTest.java | 2 +- 7 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.java diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiPrecedenceUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiPrecedenceUtil.java index 3ce82f6c773c..5e06c42d06dc 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiPrecedenceUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiPrecedenceUtil.java @@ -128,7 +128,7 @@ public class PsiPrecedenceUtil { if (expression instanceof PsiPrefixExpression) { return PREFIX_PRECEDENCE; } - if (expression instanceof PsiPostfixExpression) { + if (expression instanceof PsiPostfixExpression || expression instanceof PsiSwitchExpression) { return POSTFIX_PRECEDENCE; } if (expression instanceof PsiPolyadicExpression) { diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ReplaceExpressionUtil.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ReplaceExpressionUtil.java index 3056f5d54d88..64ab3907c4bd 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ReplaceExpressionUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/ReplaceExpressionUtil.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi.impl.source.tree.java; import com.intellij.lang.ASTNode; @@ -143,7 +129,7 @@ public class ReplaceExpressionUtil { else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) { return 12; } - else if (i == JavaElementType.POSTFIX_EXPRESSION) { + else if (i == JavaElementType.POSTFIX_EXPRESSION || i == JavaElementType.SWITCH_EXPRESSION) { return 13; } else if (i == JavaElementType.LITERAL_EXPRESSION || diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.after.java new file mode 100644 index 000000000000..f4e4f5b3e21f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.after.java @@ -0,0 +1,8 @@ +class SwitchExpression { + + void it() { + long z = (long) switch(1) { + default -> 10; + } + 1; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.java new file mode 100644 index 000000000000..ae8e8b4cf0a9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/parentheses/SwitchExpression.java @@ -0,0 +1,8 @@ +class SwitchExpression { + + void it() { + long z = (long) (switch(1) { + default -> 10; + }) + 1; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java index f88e28e1b184..2d67174e90d4 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_parentheses/UnnecessaryParenthesesInspection.java @@ -6,6 +6,16 @@ import java.util.ArrayList; public class UnnecessaryParenthesesInspection { + void switchExpressions() { + String s = (switch(1) { + case 1 -> "one"; + default -> "other"; + }).substring(1); + int z = -(switch(1) { + default -> 10; + }) + 10; + } + public int foo() { final String s = "foo" + (3 + 4); // do not warn here diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/parenthesis/UnnecessaryParenthesesQuickFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/parenthesis/UnnecessaryParenthesesQuickFixTest.java index d8e451eaed77..847bd720adc7 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/parenthesis/UnnecessaryParenthesesQuickFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/parenthesis/UnnecessaryParenthesesQuickFixTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ig.fixes.parenthesis; import com.siyeh.InspectionGadgetsBundle; @@ -47,6 +33,8 @@ public class UnnecessaryParenthesesQuickFixTest extends IGQuickFixesTestCase { public void testLambdaCast() { doTest(); } public void testLambdaBody() { doTest(); } public void testDivision() { doTest(); } + public void testSwitchExpression() { doTest(); } + @Override protected BaseInspection getInspection() { return new UnnecessaryParenthesesInspection(); diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java index 0743095d0bc3..cfa6dc951fb4 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryParenthesesInspectionTest.java @@ -18,7 +18,7 @@ public class UnnecessaryParenthesesInspectionTest extends LightCodeInsightFixtur @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_8; + return JAVA_12; } public void testUnnecessaryParenthesesInspection() {