From d7a7e0770ec8fd246e338c362267fe263deaa240 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 6 Dec 2018 16:11:10 +0100 Subject: [PATCH] IG/IPP: fix extract increment/decrement to separate statement (IDEA-203698) --- ...ntDecrementUsedAsExpressionInspection.java | 30 +++++++++---------- .../extractIncrement/SwitchExpression.java | 7 +++++ .../SwitchExpression_after.java | 10 +++++++ .../ExtractIncrementAndDecrementTest.java | 18 ++--------- 4 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression_after.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/IncrementDecrementUsedAsExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/IncrementDecrementUsedAsExpressionInspection.java index 7d4df6974a8f..e3fba3700420 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/IncrementDecrementUsedAsExpressionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/IncrementDecrementUsedAsExpressionInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2018 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.PsiReplacementUtil; +import com.siyeh.ig.psiutils.ExpressionUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -143,24 +144,27 @@ public class IncrementDecrementUsedAsExpressionInspection return; } final Project project = element.getProject(); - final PsiElementFactory factory = - JavaPsiFacade.getInstance(project).getElementFactory(); + final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final String newStatementText = element.getText() + ';'; final String operandText = operand.getText(); - if (parent instanceof PsiIfStatement || - parent instanceof PsiLoopStatement) { + if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement || parent instanceof PsiSwitchLabeledRuleStatement) { // need to add braces because // in/decrement is inside braceless control statement body final StringBuilder text = new StringBuilder(); text.append('{'); - final String elementText = - PsiReplacementUtil.getElementText(statement, element, operandText); + final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText); if (element instanceof PsiPostfixExpression) { + if (parent instanceof PsiSwitchLabeledRuleStatement) { + text.append("break "); + } text.append(elementText); text.append(newStatementText); } else { text.append(newStatementText); + if (parent instanceof PsiSwitchLabeledRuleStatement) { + text.append("break "); + } text.append(elementText); } text.append('}'); @@ -323,16 +327,11 @@ public class IncrementDecrementUsedAsExpressionInspection } public static boolean isSuitableForReplacement(@NotNull PsiUnaryExpression expression) { - final PsiElement parent = expression.getParent(); - if (parent instanceof PsiExpressionStatement || - (parent instanceof PsiExpressionList && - parent.getParent() instanceof - PsiExpressionListStatement)) { + if (ExpressionUtils.isVoidContext(expression)) { return false; } final IElementType tokenType = expression.getOperationTokenType(); - if (!tokenType.equals(JavaTokenType.PLUSPLUS) && - !tokenType.equals(JavaTokenType.MINUSMINUS)) { + if (!tokenType.equals(JavaTokenType.PLUSPLUS) && !tokenType.equals(JavaTokenType.MINUSMINUS)) { return false; } return PsiTreeUtil.getParentOfType(expression, PsiStatement.class) != null; @@ -347,8 +346,7 @@ public class IncrementDecrementUsedAsExpressionInspection extends BaseInspectionVisitor { @Override - public void visitUnaryExpression( - @NotNull PsiUnaryExpression expression) { + public void visitUnaryExpression(@NotNull PsiUnaryExpression expression) { super.visitUnaryExpression(expression); if (isSuitableForReplacement(expression)) { diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression.java new file mode 100644 index 000000000000..e61b6dcafa06 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression.java @@ -0,0 +1,7 @@ +class Main { + int test(int i) { + return switch(i) { + default -> ++i; + }; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression_after.java new file mode 100644 index 000000000000..e0d577fe4613 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/extractIncrement/SwitchExpression_after.java @@ -0,0 +1,10 @@ +class Main { + int test(int i) { + return switch(i) { + default -> { + ++i; + break i; + } + }; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/extractIncrement/ExtractIncrementAndDecrementTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/extractIncrement/ExtractIncrementAndDecrementTest.java index 140ef89fab28..3a068ed030b8 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/extractIncrement/ExtractIncrementAndDecrementTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/extractIncrement/ExtractIncrementAndDecrementTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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.ipp.extractIncrement; import com.intellij.codeInsight.intention.IntentionAction; @@ -34,6 +20,8 @@ public class ExtractIncrementAndDecrementTest extends IPPTestCase { public void testPrefixIncrement() {doExtractTest("++");} + public void testSwitchExpression() {doExtractTest("++");} + public void testSingleDoWhileBody() {doExtractTest("++");} public void testDecrementInForUpdate() {doNegativeTest("--");}