fixed PY-7410 Remove redundant parenthesis: false positive for yield from in return statement

This commit is contained in:
Ekaterina Tuzova
2012-09-05 18:06:35 -04:00
parent 5650a0884d
commit 76429aeefe
13 changed files with 111 additions and 53 deletions
@@ -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());
}
}
@@ -0,0 +1,5 @@
#PY-2310
if (A and
B):
print
@@ -0,0 +1,2 @@
var = '<input type="submit" value="Yes, Delete this event."/></form>' % (
event_id)
@@ -0,0 +1,4 @@
if <weak_warning descr="Remove redundant parentheses">((True or <weak_warning descr="Remove redundant parentheses">(False)</weak_warning>))</weak_warning>:
pass
elif <weak_warning descr="Remove redundant parentheses">(True)</weak_warning>:
pass
@@ -0,0 +1,7 @@
if (A and
B and
C):
pass
if <weak_warning descr="Remove redundant parentheses">("\n")</weak_warning>:
pass
@@ -0,0 +1,5 @@
result = (
"int line1 = 1;\n"
"\n"
"int line2 = 2;\n"
)
@@ -0,0 +1,7 @@
try:
foo()
except (<weak_warning descr="Remove redundant parentheses">(A)</weak_warning>):
pass
except <weak_warning descr="Remove redundant parentheses">(A)</weak_warning> :
pass
@@ -0,0 +1,4 @@
try:
foo()
except (A, B):
pass
@@ -0,0 +1,2 @@
while <weak_warning descr="Remove redundant parentheses">(True)</weak_warning>:
pass
@@ -0,0 +1,2 @@
def method_name(in1):
return (yield from func(in1))
@@ -1,41 +0,0 @@
if <weak_warning descr="Remove redundant parentheses">((True or <weak_warning descr="Remove redundant parentheses">(False)</weak_warning>))</weak_warning>:
pass
elif <weak_warning descr="Remove redundant parentheses">(True)</weak_warning>:
pass
while <weak_warning descr="Remove redundant parentheses">(True)</weak_warning>:
pass
try:
foo()
except (<weak_warning descr="Remove redundant parentheses">(A)</weak_warning>):
pass
except <weak_warning descr="Remove redundant parentheses">(A)</weak_warning> :
pass
try:
foo()
except (A, B):
pass
if (A and
B and
C):
pass
if <weak_warning descr="Remove redundant parentheses">("\n")</weak_warning>:
pass
result = (
"int line1 = 1;\n"
"\n"
"int line2 = 2;\n"
)
#PY-2310
if (A and
B):
print
var = '<input type="submit" value="Yes, Delete this event."/></form>' % (
event_id)
@@ -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);
}
@@ -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);
}
}