negation: parenthesis for conditional (IDEA-157530)

This commit is contained in:
Anna.Kozlova
2016-07-18 10:19:24 +02:00
parent 942cc412dc
commit fc834372fd
4 changed files with 20 additions and 3 deletions
@@ -72,9 +72,10 @@ public class BoolUtils {
}
if (expression instanceof PsiConditionalExpression) {
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)expression;
return conditionalExpression.getCondition().getText() +
'?' + getNegatedExpressionText(conditionalExpression.getThenExpression()) +
':' + getNegatedExpressionText(conditionalExpression.getElseExpression());
final boolean needParenthesis = ParenthesesUtils.getPrecedence(conditionalExpression) >= precedence;
final String text = conditionalExpression.getCondition().getText() + '?' + getNegatedExpressionText(conditionalExpression.getThenExpression()) +
':' + getNegatedExpressionText(conditionalExpression.getElseExpression());
return needParenthesis ? "(" + text + ")" : text;
}
else if (isNegation(expression)) {
final PsiExpression negated = getNegated(expression);
@@ -0,0 +1,7 @@
class IssueDemo {
private String str1;
private String str2;
public boolean foo(IssueDemo c) {
return (str2 != null ? str2.equals(c.str2) : c.str2 == null) && (str1 != null ? str1.equals(c.str1) : c.str1 == null);
}
}
@@ -0,0 +1,8 @@
class IssueDemo {
private String str1;
private String str2;
public boolean foo(IssueDemo c) {
i<caret>f (str2 != null ? !str2.equals(c.str2) : c.str2 != null) return false;
return str1 != null ? str1.equals(c.str1) : c.str1 == null;
}
}
@@ -27,6 +27,7 @@ public class SimplifiableIfStatementFixTest extends IGQuickFixesTestCase {
public void testComments() { doTest(); }
public void testParentheses() { doTest(); }
public void testMoreParentheses() { doTest(); }
public void testParenthesesForConditional() { doTest(); }
public void testPrecedence() { doTest(); }
@Override