PY-19583 Fixed: Bad simplifying in condition

While inverting expression don't change operands order if original expression isn't comparison
This commit is contained in:
Semyon Proshev
2016-07-09 21:11:02 +03:00
parent 4e43a72f48
commit f352dd1129
4 changed files with 25 additions and 8 deletions
@@ -145,14 +145,18 @@ public class ChainedComparisonsQuickFix implements LocalQuickFix {
@NotNull
private static PyExpression invertExpression(@NotNull PyBinaryExpression expression, @NotNull PyElementGenerator elementGenerator) {
final PsiElement operator = expression.getPsiOperator();
final PyExpression right = expression.getRightExpression();
PyExpression left = expression.getLeftExpression();
if (isComparisonExpression(left)) {
left = invertExpression((PyBinaryExpression)left, elementGenerator);
if (isComparisonExpression(expression)) {
final PyExpression left = expression.getLeftExpression();
final PyExpression right = expression.getRightExpression();
final String newOperator = invertOperator(assertNotNull(expression.getPsiOperator()));
final PyExpression newRight = isComparisonExpression(left) ? invertExpression((PyBinaryExpression)left, elementGenerator) : left;
return elementGenerator.createBinaryExpression(newOperator, right, newRight);
}
else {
return expression;
}
final String newOperator = invertOperator(assertNotNull(operator));
return elementGenerator.createBinaryExpression(newOperator, right, left);
}
@NotNull
@@ -0,0 +1,4 @@
class A:
def foo(self):
if index >= self.current and<caret> index - self.history_length < self.current:
pass
@@ -0,0 +1,4 @@
class A:
def foo(self):
if index >= self.current > index - self.history_length:
pass
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -284,6 +284,11 @@ public class PyQuickFixTest extends PyTestCase {
doInspectionTest(PyChainedComparisonsInspection.class, PyBundle.message("QFIX.chained.comparison"), true, true);
}
// PY-19583
public void testChainedComparison6() {
doInspectionTest(PyChainedComparisonsInspection.class, "Simplify chained comparison", true, true);
}
// PY-1362, PY-2585
public void testStatementEffect() {
doInspectionTest(PyStatementEffectInspection.class, PyBundle.message("QFIX.statement.effect"), true, true);