psi: don't insert unnecessary parentheses when replacing expression (IDEA-220761)

GitOrigin-RevId: 7fe48d0f72581221a633373933b941c09b2c69ed
This commit is contained in:
Bas Leijdekkers
2019-08-22 01:50:10 +03:00
committed by intellij-monorepo-bot
parent 670c18edc5
commit ea73b88a7c
6 changed files with 34 additions and 4 deletions
@@ -1,4 +1,4 @@
// 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.
// Copyright 2000-2019 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;
@@ -38,14 +38,15 @@ public class ReplaceExpressionUtil {
else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
if (priority < parentPriority) return true;
PsiElement element = SourceTreeToPsiMap.treeElementToPsi(oldParent);
assert element != null;
IElementType opType = ((PsiPolyadicExpression)element).getOperationTokenType();
IElementType newI = newExpr.getElementType();
if (((CompositeElement)oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND) return false;
if (newI == JavaElementType.BINARY_EXPRESSION || newI == JavaElementType.POLYADIC_EXPRESSION) {
IElementType newType = ((PsiPolyadicExpression)newExpr).getOperationTokenType();
if (newType == JavaTokenType.DIV || newExpr == JavaTokenType.PERC) return true;
if (newType == JavaTokenType.DIV || newType == JavaTokenType.PERC) return true;
}
return ((CompositeElement)oldParent).getChildRole(oldExpr) != ChildRole.LOPERAND &&
opType != JavaTokenType.PLUS &&
return opType != JavaTokenType.PLUS &&
opType != JavaTokenType.ASTERISK &&
opType != JavaTokenType.ANDAND &&
opType != JavaTokenType.OROR;
@@ -0,0 +1,8 @@
// "Remove redundant cast(s)" "true"
class Test {
{
int i = 1;
int j = 2;
System.out.println(j * (i % j));
}
}
@@ -0,0 +1,8 @@
// "Remove redundant cast(s)" "true"
class Test {
{
int i = 1;
int j = 2;
System.out.println(j * ((i<caret>nt)(i % j)));
}
}
@@ -0,0 +1,6 @@
class Divistion {
void zz() {
long actual = 987987;
long roundedActual = actual / 1000 * 1000;
}
}
@@ -0,0 +1,6 @@
class Divistion {
void zz() {
long actual = 987987;
long roundedActual = (actual <caret>/ 1000) * 1000;
}
}
@@ -34,6 +34,7 @@ public class UnnecessaryParenthesesQuickFixTest extends IGQuickFixesTestCase {
public void testLambdaCast() { doTest(); }
public void testLambdaBody() { doTest(); }
public void testDivision() { doTest(); }
public void testDivision2() { doTest(); }
public void testSwitchExpression() { doTest(); }
@Override