From 76429aeefecbaa4744a18fd7c823faa4194914ec Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Wed, 5 Sep 2012 18:06:35 -0400 Subject: [PATCH] fixed PY-7410 Remove redundant parenthesis: false positive for yield from in return statement --- .../PyRedundantParenthesesInspection.java | 16 +++-- .../BooleanMultiline.py | 5 ++ .../Formatting.py | 2 + .../IfElif.py | 4 ++ .../IfMultiline.py | 7 +++ .../StringMultiline.py | 5 ++ .../TryExcept.py | 7 +++ .../TryExceptNegate.py | 4 ++ .../PyRedundantParenthesesInspection/While.py | 2 + .../YieldFrom.py | 2 + .../PyRedundantParenthesesInspection/test.py | 41 ------------ .../python/PythonInspectionsTest.java | 6 -- .../PyRedundantParenthesesInspectionTest.java | 63 +++++++++++++++++++ 13 files changed, 111 insertions(+), 53 deletions(-) create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/BooleanMultiline.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/Formatting.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/IfElif.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/IfMultiline.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/StringMultiline.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/TryExcept.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/TryExceptNegate.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/While.py create mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/YieldFrom.py delete mode 100644 python/testData/inspections/PyRedundantParenthesesInspection/test.py create mode 100644 python/testSrc/com/jetbrains/python/inspections/PyRedundantParenthesesInspectionTest.java diff --git a/python/src/com/jetbrains/python/inspections/PyRedundantParenthesesInspection.java b/python/src/com/jetbrains/python/inspections/PyRedundantParenthesesInspection.java index 1e31741b573d..ffa49566309f 100644 --- a/python/src/com/jetbrains/python/inspections/PyRedundantParenthesesInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyRedundantParenthesesInspection.java @@ -5,6 +5,7 @@ import com.intellij.codeInspection.ProblemsHolder; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.actions.RedundantParenthesesQuickFix; @@ -55,8 +56,9 @@ public class PyRedundantParenthesesInspection extends PyInspection { public void visitPyParenthesizedExpression(final PyParenthesizedExpression node) { PyExpression expression = node.getContainedExpression(); if (node.getText().contains("\n")) return; - if (expression instanceof PyReferenceExpression - || expression instanceof PyNumericLiteralExpression) { + PyYieldExpression yieldExpression = PsiTreeUtil.getParentOfType(expression, PyYieldExpression.class, false); + if (yieldExpression != null && yieldExpression.isDelegating()) return; + if (expression instanceof PyReferenceExpression || expression instanceof PyLiteralExpression) { if (myIgnorePercOperator) { PsiElement parent = node.getParent(); if (parent instanceof PyBinaryExpression) { @@ -76,12 +78,14 @@ public class PyRedundantParenthesesInspection extends PyInspection { registerProblem(node, "Remove redundant parentheses", new RedundantParenthesesQuickFix()); } else if (expression instanceof PyBinaryExpression) { + PyBinaryExpression binaryExpression = (PyBinaryExpression)expression; + if (node.getParent() instanceof PyPrefixExpression) return; - if (((PyBinaryExpression)expression).getOperator() == PyTokenTypes.AND_KEYWORD || - ((PyBinaryExpression)expression).getOperator() == PyTokenTypes.OR_KEYWORD) { - if (((PyBinaryExpression)expression).getLeftExpression() instanceof PyParenthesizedExpression && - ((PyBinaryExpression)expression).getRightExpression() instanceof PyParenthesizedExpression) { + if (binaryExpression.getOperator() == PyTokenTypes.AND_KEYWORD || + binaryExpression.getOperator() == PyTokenTypes.OR_KEYWORD) { + if (binaryExpression.getLeftExpression() instanceof PyParenthesizedExpression && + binaryExpression.getRightExpression() instanceof PyParenthesizedExpression) { registerProblem(node, "Remove redundant parentheses", new RedundantParenthesesQuickFix()); } } diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/BooleanMultiline.py b/python/testData/inspections/PyRedundantParenthesesInspection/BooleanMultiline.py new file mode 100644 index 000000000000..b6b12280b8cb --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/BooleanMultiline.py @@ -0,0 +1,5 @@ +#PY-2310 +if (A and + B): + print + diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/Formatting.py b/python/testData/inspections/PyRedundantParenthesesInspection/Formatting.py new file mode 100644 index 000000000000..2903b794d936 --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/Formatting.py @@ -0,0 +1,2 @@ +var = '' % ( +event_id) diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/IfElif.py b/python/testData/inspections/PyRedundantParenthesesInspection/IfElif.py new file mode 100644 index 000000000000..1d36ea186360 --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/IfElif.py @@ -0,0 +1,4 @@ +if ((True or (False))): + pass +elif (True): + pass diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/IfMultiline.py b/python/testData/inspections/PyRedundantParenthesesInspection/IfMultiline.py new file mode 100644 index 000000000000..5ad0df08013c --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/IfMultiline.py @@ -0,0 +1,7 @@ +if (A and + B and + C): + pass + +if ("\n"): + pass diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/StringMultiline.py b/python/testData/inspections/PyRedundantParenthesesInspection/StringMultiline.py new file mode 100644 index 000000000000..2f7736726d9a --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/StringMultiline.py @@ -0,0 +1,5 @@ +result = ( + "int line1 = 1;\n" + "\n" + "int line2 = 2;\n" +) diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/TryExcept.py b/python/testData/inspections/PyRedundantParenthesesInspection/TryExcept.py new file mode 100644 index 000000000000..c659153b82b0 --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/TryExcept.py @@ -0,0 +1,7 @@ +try: + foo() +except ((A)): + pass +except (A) : + pass + diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/TryExceptNegate.py b/python/testData/inspections/PyRedundantParenthesesInspection/TryExceptNegate.py new file mode 100644 index 000000000000..2359b0dd4b9c --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/TryExceptNegate.py @@ -0,0 +1,4 @@ +try: + foo() +except (A, B): + pass diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/While.py b/python/testData/inspections/PyRedundantParenthesesInspection/While.py new file mode 100644 index 000000000000..893868d917ec --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/While.py @@ -0,0 +1,2 @@ +while (True): + pass diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/YieldFrom.py b/python/testData/inspections/PyRedundantParenthesesInspection/YieldFrom.py new file mode 100644 index 000000000000..d1e744002606 --- /dev/null +++ b/python/testData/inspections/PyRedundantParenthesesInspection/YieldFrom.py @@ -0,0 +1,2 @@ +def method_name(in1): + return (yield from func(in1)) \ No newline at end of file diff --git a/python/testData/inspections/PyRedundantParenthesesInspection/test.py b/python/testData/inspections/PyRedundantParenthesesInspection/test.py deleted file mode 100644 index 124560ad41aa..000000000000 --- a/python/testData/inspections/PyRedundantParenthesesInspection/test.py +++ /dev/null @@ -1,41 +0,0 @@ -if ((True or (False))): - pass -elif (True): - pass - -while (True): - pass - -try: - foo() -except ((A)): - pass -except (A) : - pass - -try: - foo() -except (A, B): - pass - -if (A and - B and - C): - pass - -if ("\n"): - pass - -result = ( - "int line1 = 1;\n" - "\n" - "int line2 = 2;\n" -) - -#PY-2310 -if (A and - B): - print - -var = '' % ( -event_id) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java index 2952720da43d..8d3549a72bf9 100644 --- a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java +++ b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java @@ -260,12 +260,6 @@ public class PythonInspectionsTest extends PyTestCase { doHighlightingTest(PyChainedComparisonsInspection.class); } - public void testPyRedundantParenthesesInspection() { - myFixture.configureByFile("inspections/PyRedundantParenthesesInspection/test.py"); - myFixture.enableInspections(PyRedundantParenthesesInspection.class); - myFixture.checkHighlighting(true, false, true); - } - public void testPyBroadExceptionInspection() { doHighlightingTest(PyBroadExceptionInspection.class); } diff --git a/python/testSrc/com/jetbrains/python/inspections/PyRedundantParenthesesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyRedundantParenthesesInspectionTest.java new file mode 100644 index 000000000000..7ecc8671b249 --- /dev/null +++ b/python/testSrc/com/jetbrains/python/inspections/PyRedundantParenthesesInspectionTest.java @@ -0,0 +1,63 @@ +package com.jetbrains.python.inspections; + +import com.jetbrains.python.fixtures.PyTestCase; +import com.jetbrains.python.psi.LanguageLevel; + +/** + * @author yole + */ +public class PyRedundantParenthesesInspectionTest extends PyTestCase { + public void doTest() { + myFixture.configureByFile("inspections/PyRedundantParenthesesInspection/" + getTestName(false) + ".py"); + myFixture.enableInspections(PyRedundantParenthesesInspection.class); + myFixture.checkHighlighting(true, false, true); + } + + public void doTest(LanguageLevel languageLevel) { + try { + setLanguageLevel(languageLevel); + myFixture.configureByFile("inspections/PyRedundantParenthesesInspection/" + getTestName(false) + ".py"); + myFixture.enableInspections(PyRedundantParenthesesInspection.class); + myFixture.checkHighlighting(true, false, true); + } finally { + setLanguageLevel(null); + } + } + + public void testBooleanMultiline() { + doTest(); + } + + public void testFormatting() { + doTest(); + } + + public void testIfElif() { + doTest(); + } + + public void testIfMultiline() { + doTest(); + } + + public void testStringMultiline() { + doTest(); + } + + public void testTryExcept() { + doTest(); + } + + public void testTryExceptNegate() { + doTest(); + } + + public void testWhile() { + doTest(); + } + + public void testYieldFrom() { //PY-7410 + doTest(LanguageLevel.PYTHON33); + } + +}