ReplaceShiftWithMultiplyIntention: support parentheses; tests

This commit is contained in:
Tagir Valeev
2018-07-10 14:13:39 +07:00
parent dc328949c7
commit 69e1ff670f
11 changed files with 62 additions and 3 deletions
@@ -17,6 +17,7 @@ package com.siyeh.ipp.shift;
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;
@@ -69,7 +70,7 @@ public class ReplaceShiftWithMultiplyIntention extends MutablyNamedIntention {
}
@Override
public void processIntention(PsiElement element) {
public void processIntention(@NotNull PsiElement element) {
if (element instanceof PsiBinaryExpression) {
replaceShiftWithMultiplyOrDivide(element);
}
@@ -100,7 +101,7 @@ public class ReplaceShiftWithMultiplyIntention extends MutablyNamedIntention {
private static void replaceShiftWithMultiplyOrDivide(PsiElement element) {
final PsiBinaryExpression exp = (PsiBinaryExpression)element;
final PsiExpression lhs = exp.getLOperand();
final PsiExpression rhs = exp.getROperand();
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(exp.getROperand());
final IElementType tokenType = exp.getOperationTokenType();
final String operatorString;
if (tokenType.equals(JavaTokenType.LTLT)) {
@@ -17,6 +17,7 @@ package com.siyeh.ipp.shift;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ipp.base.PsiElementPredicate;
class ShiftByLiteralPredicate implements PsiElementPredicate {
@@ -68,7 +69,7 @@ class ShiftByLiteralPredicate implements PsiElementPredicate {
if (!ShiftUtils.isIntegral(lhsType)) {
return false;
}
final PsiExpression rhs = expression.getROperand();
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(expression.getROperand());
return ShiftUtils.isIntLiteral(rhs);
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo << 1<caret>2;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
foo <<= 1<caret>2;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
foo *= 4096<caret>;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo * 4096<caret>;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo << (2<caret>4);
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo * 16777<caret>216;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo >> 1<caret>2;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int foo) {
int x = foo / 4096<caret>;
}
}
@@ -0,0 +1,17 @@
// 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.shift;
import com.siyeh.ipp.IPPTestCase;
public class ReplaceShiftWithMultiplyIntentionTest extends IPPTestCase {
public void testLeftShift() { doTest("Replace '<<' with '*'"); }
public void testLeftShiftAssign() { doTest("Replace '<<=' with '*='"); }
public void testParentheses() { doTest("Replace '<<' with '*'"); }
public void testRightShift() { doTest("Replace '>>' with '/'"); }
@Override
protected String getRelativePath() {
return "shift/replace_shift_with_multiply";
}
}