Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch RET504 usages via decorators #3395

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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