Skip to content

Commit

Permalink
Catch RET504 usages via decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 8, 2023
1 parent 3f04def commit a6f298a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 20 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_return/RET504.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,23 @@ def inner():
nonlocal X
X = 1
return X


def decorator() -> Flask:
app = Flask(__name__)

@app.route('/hello')
def hello() -> str:
"""Hello endpoint."""
return 'Hello, World!'

return app


def default():
y = 1

def f(x = y) -> X:
return x

return y
22 changes: 20 additions & 2 deletions crates/ruff/src/rules/flake8_return/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,26 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
.non_locals
.extend(names.iter().map(String::as_str));
}
StmtKind::FunctionDef { .. } | StmtKind::AsyncFunctionDef { .. } => {
// Don't recurse.
StmtKind::FunctionDef {
decorator_list,
args,
returns,
..
}
| StmtKind::AsyncFunctionDef {
decorator_list,
args,
returns,
..
} => {
// Don't recurse into the body, but visit the decorators, etc.
for expr in decorator_list {
visitor::walk_expr(self, expr);
}
if let Some(returns) = returns {
visitor::walk_expr(self, returns);
}
visitor::walk_arguments(self, args);
}
StmtKind::Return { value } => {
self.stack
Expand Down

0 comments on commit a6f298a

Please sign in to comment.