PY-20324 Don't report parentheses around returned tuples with unpacking

This commit is contained in:
Mikhail Golubev
2018-03-21 17:28:35 +03:00
parent 8943637993
commit 39358aeb12
3 changed files with 20 additions and 2 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.SmartSerializer;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyTokenTypes;
@@ -34,6 +35,8 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* User: catherine
* <p>
@@ -100,10 +103,15 @@ public class PyRedundantParenthesesInspection extends PyInspection {
registerProblem(node, PyBundle.message("QFIX.redundant.parentheses"), new RedundantParenthesesQuickFix());
}
else if (node.getParent() instanceof PyIfPart ||
node.getParent() instanceof PyWhilePart ||
node.getParent() instanceof PyReturnStatement) {
node.getParent() instanceof PyWhilePart) {
registerProblem(node, PyBundle.message("QFIX.redundant.parentheses"), new RedundantParenthesesQuickFix());
}
else if (node.getParent() instanceof PyReturnStatement) {
final PyTupleExpression tuple = as(expression, PyTupleExpression.class);
if (!(tuple != null && ContainerUtil.or(tuple.getElements(), PyStarExpression.class::isInstance))) {
registerProblem(node, PyBundle.message("QFIX.redundant.parentheses"), new RedundantParenthesesQuickFix());
}
}
else if (expression instanceof PyBinaryExpression) {
final PyBinaryExpression binaryExpression = (PyBinaryExpression)expression;
@@ -0,0 +1,6 @@
def f1(x):
return (x, *x)
def f2(x):
return <weak_warning descr="Remove redundant parentheses">(x, x)</weak_warning>
@@ -78,4 +78,8 @@ public class PyRedundantParenthesesInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-20324
public void testReturn() {
runWithLanguageLevel(LanguageLevel.PYTHON35, this::doTest);
}
}