From 05253be08ad59320942551733db14d2fe12c637f Mon Sep 17 00:00:00 2001 From: Valentina Kiryushkina Date: Thu, 14 Jan 2016 17:46:28 +0300 Subject: [PATCH] PY-18203 Redundant parentheses quick fix produces syntactically incorrect code for tuples If parentheses expression contains binary expression where left and right are parentheses expressions too, we check if left and right contain tuples --- .../inspections/quickfix/RedundantParenthesesQuickFix.java | 3 ++- python/testData/inspections/RedundantParenthesesInTuples.py | 1 + .../inspections/RedundantParenthesesInTuples_after.py | 1 + python/testSrc/com/jetbrains/python/PyQuickFixTest.java | 5 +++++ 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 python/testData/inspections/RedundantParenthesesInTuples.py create mode 100644 python/testData/inspections/RedundantParenthesesInTuples_after.py diff --git a/python/src/com/jetbrains/python/inspections/quickfix/RedundantParenthesesQuickFix.java b/python/src/com/jetbrains/python/inspections/quickfix/RedundantParenthesesQuickFix.java index 7224cccbf4d9..3c4343420c54 100644 --- a/python/src/com/jetbrains/python/inspections/quickfix/RedundantParenthesesQuickFix.java +++ b/python/src/com/jetbrains/python/inspections/quickfix/RedundantParenthesesQuickFix.java @@ -72,7 +72,8 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix { 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; diff --git a/python/testData/inspections/RedundantParenthesesInTuples.py b/python/testData/inspections/RedundantParenthesesInTuples.py new file mode 100644 index 000000000000..668372db89b9 --- /dev/null +++ b/python/testData/inspections/RedundantParenthesesInTuples.py @@ -0,0 +1 @@ +print("%d%s%s" % (((1,) + ("", "")))) \ 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 index 000000000000..31ac4040ce02 --- /dev/null +++ b/python/testData/inspections/RedundantParenthesesInTuples_after.py @@ -0,0 +1 @@ +print("%d%s%s" % ((1,) + ("", ""))) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java index dded82dd8930..96deaf719141 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -244,6 +244,11 @@ public class PyQuickFixTest extends PyTestCase { 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);