mirror of https://github.com/astral-sh/ruff
Ignore generators in flake8-return rules (#2126)
We could do a better job of handling them, but they cause too many false-positives right now. Closes #2119.
This commit is contained in:
parent
24d0a980c5
commit
930c3be69d
|
|
@ -114,3 +114,12 @@ def bar3(x, y, z):
|
|||
else:
|
||||
return z
|
||||
return None
|
||||
|
||||
|
||||
def prompts(self, foo):
|
||||
if not foo:
|
||||
return []
|
||||
|
||||
for x in foo:
|
||||
yield x
|
||||
yield x + 1
|
||||
|
|
|
|||
|
|
@ -325,6 +325,11 @@ pub fn function(checker: &mut Checker, body: &[Stmt]) {
|
|||
visitor.stack
|
||||
};
|
||||
|
||||
// Avoid false positives for generators.
|
||||
if !stack.yields.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
if checker.settings.rules.enabled(&Rule::SuperfluousElseReturn)
|
||||
|| checker.settings.rules.enabled(&Rule::SuperfluousElseRaise)
|
||||
|| checker
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use crate::ast::visitor::Visitor;
|
|||
#[derive(Default)]
|
||||
pub struct Stack<'a> {
|
||||
pub returns: Vec<(&'a Stmt, Option<&'a Expr>)>,
|
||||
pub yields: Vec<&'a Expr>,
|
||||
pub ifs: Vec<&'a Stmt>,
|
||||
pub elifs: Vec<&'a Stmt>,
|
||||
pub refs: FxHashMap<&'a str, Vec<Location>>,
|
||||
|
|
@ -116,7 +117,6 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
|||
.push((stmt.location, stmt.end_location.unwrap()));
|
||||
visitor::walk_stmt(self, stmt);
|
||||
}
|
||||
|
||||
_ => {
|
||||
visitor::walk_stmt(self, stmt);
|
||||
}
|
||||
|
|
@ -143,8 +143,8 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
|||
.or_insert_with(Vec::new)
|
||||
.push(expr.location);
|
||||
}
|
||||
ExprKind::JoinedStr { .. } => {
|
||||
visitor::walk_expr(self, expr);
|
||||
ExprKind::YieldFrom { .. } | ExprKind::Yield { .. } => {
|
||||
self.stack.yields.push(expr);
|
||||
}
|
||||
_ => visitor::walk_expr(self, expr),
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue