diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntention.java index b8208472f921..d92c8850216a 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntention.java @@ -17,6 +17,7 @@ package com.siyeh.ipp.opassign; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiUtil; import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.psiutils.CommentTracker; @@ -24,8 +25,7 @@ import com.siyeh.ipp.base.MutablyNamedIntention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; -public class ReplaceAssignmentWithPostfixExpressionIntention - extends MutablyNamedIntention { +public class ReplaceAssignmentWithPostfixExpressionIntention extends MutablyNamedIntention { @NotNull @Override @@ -38,7 +38,7 @@ public class ReplaceAssignmentWithPostfixExpressionIntention final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)element; final PsiBinaryExpression rhs = - (PsiBinaryExpression)assignmentExpression.getRExpression(); + (PsiBinaryExpression)PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression()); final PsiExpression lhs = assignmentExpression.getLExpression(); final String lhsText = lhs.getText(); final IElementType tokenType; @@ -67,7 +67,7 @@ public class ReplaceAssignmentWithPostfixExpressionIntention final PsiExpression lhs = assignmentExpression.getLExpression(); CommentTracker commentTracker = new CommentTracker(); final String lhsText = commentTracker.text(lhs); - final PsiExpression rhs = assignmentExpression.getRExpression(); + final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression()); if (!(rhs instanceof PsiBinaryExpression)) { return; } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java index 89cd997e6822..2d4c0d4e64a0 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionPredicate.java @@ -17,14 +17,14 @@ package com.siyeh.ipp.opassign; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiUtil; +import com.siyeh.ig.psiutils.ExpressionUtils; import com.siyeh.ig.psiutils.ParenthesesUtils; import com.siyeh.ig.psiutils.VariableAccessUtils; import com.siyeh.ipp.base.PsiElementPredicate; class ReplaceAssignmentWithPostfixExpressionPredicate implements PsiElementPredicate { - private static final Integer ONE = Integer.valueOf(1); - public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiAssignmentExpression)) { return false; @@ -44,34 +44,21 @@ class ReplaceAssignmentWithPostfixExpressionPredicate implements PsiElementPredi return false; } final PsiVariable variable = (PsiVariable)target; - final PsiExpression rhs = assignmentExpression.getRExpression(); + final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression()); if (!(rhs instanceof PsiBinaryExpression)) { return false; } final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)rhs; - final PsiExpression rOperand = binaryExpression.getROperand(); - if (rOperand == null) { - return false; - } - final PsiExpression lOperand = binaryExpression.getLOperand(); + final PsiExpression lOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getLOperand()); + final PsiExpression rOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getROperand()); final IElementType tokenType = binaryExpression.getOperationTokenType(); - if (lOperand instanceof PsiLiteral) { - final PsiLiteral literal = (PsiLiteral)lOperand; - final Object value = literal.getValue(); - if (ONE != value) { - return false; - } + if (ExpressionUtils.isLiteral(lOperand, 1)) { if (!VariableAccessUtils.evaluatesToVariable(rOperand, variable)) { return false; } return JavaTokenType.PLUS.equals(tokenType); } - else if (rOperand instanceof PsiLiteral) { - final PsiLiteral literal = (PsiLiteral)rOperand; - final Object value = literal.getValue(); - if (ONE != value) { - return false; - } + else if (ExpressionUtils.isLiteral(rOperand, 1)) { if (!VariableAccessUtils.evaluatesToVariable(lOperand, variable)) { return false; } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses.java new file mode 100644 index 000000000000..d0ba725e5d66 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses.java @@ -0,0 +1,5 @@ +class Test { + void test(int i) { + i = ((i)+(1)); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses_after.java new file mode 100644 index 000000000000..16fe4de0fa6d --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Parentheses_after.java @@ -0,0 +1,5 @@ +class Test { + void test(int i) { + i++; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple.java new file mode 100644 index 000000000000..8884b21a059c --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple.java @@ -0,0 +1,5 @@ +class Test { + void test(int i) { + i = i+1; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple_after.java new file mode 100644 index 000000000000..754437d461b5 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/assignment_to_postfix/Simple_after.java @@ -0,0 +1,5 @@ +class Test { + void test(int i) { + i++; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntentionTest.java new file mode 100644 index 000000000000..8ac9e7829e2c --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/ReplaceAssignmentWithPostfixExpressionIntentionTest.java @@ -0,0 +1,23 @@ +// 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.opassign; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +/** + * @see ReplaceAssignmentWithPostfixExpressionIntention + */ +public class ReplaceAssignmentWithPostfixExpressionIntentionTest extends IPPTestCase { + public void testSimple() { doTest(); } + public void testParentheses() { doTest(); } + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("replace.some.operator.with.other.intention.name", "=", "i++"); + } + + @Override + protected String getRelativePath() { + return "opassign/assignment_to_postfix"; + } +}