[syntax-errors] return outside function (#17300)

Summary
--

This PR reimplements [return-outside-function
(F706)](https://docs.astral.sh/ruff/rules/return-outside-function/) as a
semantic syntax error.

These changes are very similar to those in
https://github.com/astral-sh/ruff/pull/17298.

Test Plan
--

New linter tests, plus existing F706 tests.
This commit is contained in:
Brent Westbrook 2025-04-11 13:05:54 -04:00 committed by GitHub
parent 4bfdf54d1a
commit da32a83c9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 82 additions and 29 deletions

View file

@ -93,12 +93,15 @@ impl SemanticSyntaxChecker {
);
}
}
Stmt::Return(ast::StmtReturn {
value: Some(value), ..
}) => {
// test_err single_star_return
// def f(): return *x
Self::invalid_star_expression(value, ctx);
Stmt::Return(ast::StmtReturn { value, range }) => {
if let Some(value) = value {
// test_err single_star_return
// def f(): return *x
Self::invalid_star_expression(value, ctx);
}
if !ctx.in_function_scope() {
Self::add_error(ctx, SemanticSyntaxErrorKind::ReturnOutsideFunction, *range);
}
}
Stmt::For(ast::StmtFor { target, iter, .. }) => {
// test_err single_star_for
@ -797,6 +800,9 @@ impl Display for SemanticSyntaxError {
SemanticSyntaxErrorKind::YieldOutsideFunction(kind) => {
write!(f, "`{kind}` statement outside of a function")
}
SemanticSyntaxErrorKind::ReturnOutsideFunction => {
f.write_str("`return` statement outside of a function")
}
}
}
}
@ -1092,6 +1098,9 @@ pub enum SemanticSyntaxErrorKind {
/// [PEP 492]: https://peps.python.org/pep-0492/
/// [PEP 530]: https://peps.python.org/pep-0530/
YieldOutsideFunction(YieldOutsideFunctionKind),
/// Represents the use of `return` outside of a function scope.
ReturnOutsideFunction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]