mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user