PY-25784 Warn about use of star expressions in return and yield statements

False positive from "Redundant parentheses" inspection has been fixed
earlier for PY-20324.
This commit is contained in:
Mikhail Golubev
2018-03-21 17:28:35 +03:00
parent 39358aeb12
commit 33c6ca2189
2 changed files with 29 additions and 2 deletions
@@ -15,8 +15,13 @@
*/
package com.jetbrains.python.validation;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.codeInsight.functionTypeComments.psi.PyParameterTypeList;
import com.jetbrains.python.psi.PyReturnStatement;
import com.jetbrains.python.psi.PyStarExpression;
import com.jetbrains.python.psi.PyTupleExpression;
import com.jetbrains.python.psi.PyYieldExpression;
import org.jetbrains.annotations.NotNull;
/**
* @author yole
@@ -25,8 +30,21 @@ public class StarAnnotator extends PyAnnotator {
@Override
public void visitPyStarExpression(PyStarExpression node) {
super.visitPyStarExpression(node);
if (!node.isAssignmentTarget() && !node.isUnpacking() && !(node.getParent() instanceof PyParameterTypeList)) {
if (!node.isAssignmentTarget() && !allowedUnpacking(node) && !(node.getParent() instanceof PyParameterTypeList)) {
getHolder().createErrorAnnotation(node, "Can't use starred expression here");
}
}
private static boolean allowedUnpacking(@NotNull PyStarExpression starExpression) {
if (!starExpression.isUnpacking()) {
return false;
}
final PsiElement parent = starExpression.getParent();
if (parent instanceof PyTupleExpression && (parent.getParent() instanceof PyReturnStatement ||
parent.getParent() instanceof PyYieldExpression)) {
return false;
}
return true;
}
}
+10 -1
View File
@@ -7,4 +7,13 @@ if <error descr="Can't use starred expression here">*x</error>:
pass
1 + (<error descr="Can't use starred expression here">*x</error>)
1 + (*x,)
1 + (*x,)
def f(x):
return x, <error descr="Can't use starred expression here">*x</error>
def g(x):
yield from x, <error descr="Can't use starred expression here">*x</error>
yield x, <error descr="Can't use starred expression here">*x</error>