Commit 69e1ff67 authored by Tagir Valeev's avatar Tagir Valeev
Browse files

ReplaceShiftWithMultiplyIntention: support parentheses; tests

parent dc328949
Showing with 62 additions and 3 deletions
+62 -3
......@@ -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);
}
}
class Test {
void test(int foo) {
int x = foo << 1<caret>2;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
foo <<= 1<caret>2;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
foo *= 4096<caret>;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
int x = foo * 4096<caret>;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
int x = foo << (2<caret>4);
}
}
\ No newline at end of file
class Test {
void test(int foo) {
int x = foo * 16777<caret>216;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
int x = foo >> 1<caret>2;
}
}
\ No newline at end of file
class Test {
void test(int foo) {
int x = foo / 4096<caret>;
}
}
\ No newline at end of file
// 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";
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment