PY-31795 Don't suggest removing parentheses when there is no expression inside

This commit is contained in:
Mikhail Golubev
2018-09-25 15:56:54 +03:00
parent 0ebde2269f
commit 2289223414
8 changed files with 30 additions and 6 deletions
@@ -82,8 +82,9 @@ public class PyRedundantParenthesesInspection extends PyInspection {
@Override
public void visitPyParenthesizedExpression(final PyParenthesizedExpression node) {
final PyExpression expression = node.getContainedExpression();
if (node.textContains('\n')) return;
final PyExpression expression = node.getContainedExpression();
if (expression == null) return;
final PyYieldExpression yieldExpression = PsiTreeUtil.getParentOfType(expression, PyYieldExpression.class, false);
if (yieldExpression != null) return;
if (expression instanceof PyTupleExpression && myIgnoreTupleInReturn) {
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.NotNull;
/**
@@ -38,11 +39,9 @@ public class RedundantParenthesesQuickFix implements LocalQuickFix {
}
}
else {
while (element instanceof PyParenthesizedExpression) {
PyExpression expression = ((PyParenthesizedExpression)element).getContainedExpression();
if (expression != null) {
element = element.replace(expression);
}
final PyExpression content = PyPsiUtils.flattenParens((PyParenthesizedExpression)element);
if (content != null) {
element.replace(content);
}
}
}
@@ -0,0 +1 @@
if <caret>(<EOLError descr="Indent expected"></EOLError><EOLError descr="')' expected"></EOLError>
@@ -0,0 +1,7 @@
def pycharm_hanger_method(a, b):
if(<error descr="Unexpected expression syntax">!m_<caret>isReverse</error>)<error descr="Colon expected">{
m_startPoint = startPoint
} else {
m_endPoint = startPoint
}</error>
@@ -0,0 +1 @@
(<caret>((42)))
@@ -248,6 +248,10 @@ public class PyQuickFixTest extends PyTestCase {
doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
}
public void testRedundantParenthesesMultipleParentheses() {
doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
}
// PY-15506
public void testEmptyListOfBaseClasses() {
doInspectionTest(PyRedundantParenthesesInspection.class, PyBundle.message("QFIX.redundant.parentheses"), true, true);
@@ -78,6 +78,16 @@ public class PyRedundantParenthesesInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-31795
public void testSyntaxErrorInside() {
doTest();
}
// PY-31795
public void testSingleLeftParenthesis() {
doTest();
}
// PY-20324
public void testReturn() {
runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest);