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:
Charlie Marsh 2023-01-24 08:15:26 -05:00 committed by GitHub
parent 24d0a980c5
commit 930c3be69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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),
}