PY-18203 Redundant parentheses quick fix produces syntactically incorrect code for...
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Thu, 14 Jan 2016 14:46:28 +0000 (17:46 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Thu, 14 Jan 2016 14:46:28 +0000 (17:46 +0300)
If parentheses expression contains binary expression where left and right are parentheses expressions too, we check if left and right contain tuples

python/src/com/jetbrains/python/inspections/quickfix/RedundantParenthesesQuickFix.java
python/testData/inspections/RedundantParenthesesInTuples.py [new file with mode: 0644]
python/testData/inspections/RedundantParenthesesInTuples_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/PyQuickFixTest.java

index 7224cccbf4d9463c6626eb14cd6d2494a73c8dde..3c4343420c5428ee9c64a7cee3af9c39f9654fa5 100644 (file)
@@ -72,7 +72,8 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix {
         right instanceof PyParenthesizedExpression) {
       PyExpression leftContained = ((PyParenthesizedExpression)left).getContainedExpression();
       PyExpression rightContained = ((PyParenthesizedExpression)right).getContainedExpression();
         right instanceof PyParenthesizedExpression) {
       PyExpression leftContained = ((PyParenthesizedExpression)left).getContainedExpression();
       PyExpression rightContained = ((PyParenthesizedExpression)right).getContainedExpression();
-      if (leftContained != null && rightContained != null) {
+      if (leftContained != null && rightContained != null &&
+          !(leftContained instanceof PyTupleExpression) && !(rightContained instanceof PyTupleExpression)) {
         left.replace(leftContained);
         right.replace(rightContained);
         return true;
         left.replace(leftContained);
         right.replace(rightContained);
         return true;
diff --git a/python/testData/inspections/RedundantParenthesesInTuples.py b/python/testData/inspections/RedundantParenthesesInTuples.py
new file mode 100644 (file)
index 0000000..668372d
--- /dev/null
@@ -0,0 +1 @@
+print("%d%s%s" % (((1,) + <caret>("", ""))))
\ No newline at end of file
diff --git a/python/testData/inspections/RedundantParenthesesInTuples_after.py b/python/testData/inspections/RedundantParenthesesInTuples_after.py
new file mode 100644 (file)
index 0000000..31ac404
--- /dev/null
@@ -0,0 +1 @@
+print("%d%s%s" % ((1,) + ("", "")))
\ No newline at end of file
index dded82dd8930f605e7e677728b85c54f460f3a44..96deaf71914160348ce309b1d8db01dae4ebb4c0 100644 (file)
@@ -244,6 +244,11 @@ public class PyQuickFixTest extends PyTestCase {
     doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
   }
 
     doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
   }
 
+  // PY-18203
+  public void testRedundantParenthesesInTuples() {
+    doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
+  }
+
   // PY-1020
   public void testChainedComparisons() {
     doInspectionTest(PyChainedComparisonsInspection.class, PyBundle.message("QFIX.chained.comparison"), true, true);
   // PY-1020
   public void testChainedComparisons() {
     doInspectionTest(PyChainedComparisonsInspection.class, PyBundle.message("QFIX.chained.comparison"), true, true);